JS Tutorials
JS Objects
JS Functions
JS Classes
JS Aysnc
HTML events are "things" that happen to HTML elements.
When JavaScript is used in HTML pages, JavaScript can "react" on these events.
An HTML event can be something the browser does, or something a user does.
Here are some examples of HTML events:
Often, when events happen, you may want to do something.
JavaScript lets you execute code when events are detected.
HTML allows event handler attributes, with JavaScript code, to be added to HTML elements.
With single quotes:
<element
event='some
JavaScript'>
With double quotes:
<element
event="some
JavaScript">
In the following example, an onclick
attribute (with code),
is added to a
<button>
element:
<button onclick="document.getElementById('demo').innerHTML =
Date()">The time is?</button>
In the example above, the JavaScript code changes the content of the element with id="demo".
In the next example, the code changes the content of
its own element (using this.innerHTML
):
<button onclick="this.innerHTML = Date()">The time is?</button>