HTML 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.
You canuse either single or double quotation marks:
<element event='some JavaScript'>

<element event="some JavaScript">

In the following example, an onclick attribute (with code), is added to a <button> element:

In the example above, the JavaScript code changes the content of the element with id="time1". In the next example, the code changes the content of its own element (using this.innerHTML):


Notice here the time is displayed in/on the button rather than in a new line/paragraph. You can choose which one you prefer for your purposes.

JavaScript code is often several lines long. It is more common to see event attributes calling functions:

In this third example the time is displayed using functions:

Common HTML Events

Here is a list of some common HTML events: Event Description

  1. onchange - An HTML element has been changed
  2. onclick - The user clicks an HTML element
  3. onmouseover - The user moves the mouse over an HTML element
  4. onmouseout - The user moves the mouse away from an HTML element
  5. ondblclick - The user double clicks the button
  6. onwheel - When the mouse wheel is rolled over the button
See how each is works below

1

2

3

4

5

6



There are many other options. For more see here: Full list of HTML DOM Events