HTML DOM allows JavaScript to react to HTML events.

HTML DOM events syntax

JavaScript can be executed when an event occurs, such as when a user clicks on an HTML element.

To execute code when the user clicks on an element, add JavaScript code to the HTML event attribute:

onclick=JavaScript

Example of HTML event:

When the user clicks Mouse time

When the web page has loaded

When the image has loaded

When the mouse moves over the element

When the input field is changed

When the HTML form is submitted

When the user triggers a keypress

HTML DOM events example

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
</head>
<body>

<h1 onclick="this.innerHTML='Ooops!'">点击文本!</h1>

</body>
</html>

Run instance »

Click the "Run instance" button to view the online instance