Why do we not use the <script> tag for calling the function 'alert 'in HTML? -


for popping alert box on button click,we use alert method javascript, right?

i haven't seen code surrounds call alert inside onclick attribute in '<script>' tag.

event attributes

https://developer.mozilla.org/en-us/docs/web/guide/html/event_attributes

that's cause onclick inside html tag is read browser javascript

<a id='el' onclick=' /*everything here js*/ alert("hey"); '>click me alert</a> 

otherwise you'd preferably use script tags before </body> tag:

<a id='el'>click me alert</a>  <script>     var el = document.getelementbyid("el");     el.onclick = function(){        alert("hey!");     }; </script> 

or using addeventlistener method on "click" event :

<a id='el'>click me alert</a>  <script>     var el = document.getelementbyid("el");     el.addeventlistener("click", function(){         alert("hey!");     }); </script> 

Comments