Element click handler
HTML elements can do something when they are clicked. This is because a JavaScript function can be connected to an HTML element. Here is one snippet in which the JavaScript function associated with the element is specified in the HTML:
<!DOCTYPE html>
<html>
<body>
<div id="one" onclick="alert('Ouch! Stop it!')">Don't click here!</div>
</body>
</html>
Whenever the text in the div
gets clicked, a pop up with the text Ouch! Stop it!
opens. Here, the JavaScript is specified directly after onclick
, but if there is JavaScript on the page, you can also refer to a function that's in that JavaScript like this:
<!DOCTYPE html>
<html>
<body>
<script>
function stop(){
alert("Ouch! Stop it!");
}
</script>
<div id="one" onclick="stop()">Don't click here!</div>
...