This site is mobile accessible. Press the "Tap Here" button to use a different font-size.
Smartphone icons created by Freepik - Flaticon
3.4 JS Events
- HTML events are "things" that happen to HTML elements.
- When JavaScript is used in HTML pages, JavaScript can "react" on these events.
HTML Events
- An HTML event can be something the browser does, or something a user does.
- Here are some examples of HTML events:
- An HTML web page has finished loading
- An HTML input field was changed
- An HTML button was clicked
- 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>
JavaScript code is often several lines long. It is more common to see event attributes calling functions:
<button onclick="displayDate()">The time is?</button>
Common HTML Events
Here is a list of some common HTML events:
Event |
Description |
onchange |
An HTML element has been changed |
onclick |
The user clicks an HTML element |
onmouseover |
The user moves the mouse over an HTML element |
onmouseout |
The user moves the mouse away from an HTML element |
onkeydown |
The user pushes a keyboard key |
onload |
The browser has finished loading the page |
The list is much longer: W3Schools JavaScript Reference HTML DOM Events.
What can JavaScript Do?
Event handlers can be used to handle, and verify, user input, user actions, and browser actions:
- Things that should be done every time a page loads
- Things that should be done when the page is closed
- Action that should be performed when a user clicks a button
- Content that should be verified when a user inputs data
- And more ...
Many different methods can be used to let JavaScript work with events:
- HTML event attributes can execute JavaScript code directly
- HTML event attributes can call JavaScript functions
- You can assign your own event handler functions to HTML elements
- You can prevent events from being sent or being handled
- And more ...
Learn a lot more about events and event handlers in the HTML DOM chapters.
Module 3. Data Types, Functions, Objects and Events
JavaScript icons used in the buttons provided by ICONS8.COM. Smartphone icons created by Freepik - Flaticon
Example files created in this module:
JS Data Types part 1
JS Data Types part 2
JS Data Types part 3
JS Data Types part 4
JS Data Types part 5
JS Data Types - Strings part 1
JS Data Types - Strings part 2
JS Data Types - Numbers part 1
JS Data Types - Numbers part 2
JS Data Types - Booleans
JS Data Types - Arrays
JS Data Types - Objects
JS Operators - typeof part 1
JS Operators - typeof part 2
JS Operators - typeof part 3
JS Operators - typeof part 4
JS Operators - typeof: Empty
JS Operators - typeof: Null
JS Operators - typeof: Undefined
JS Operators - typeof: Undefined/Null Difference
JS Data Types - Primitive Data
JS Data Types - Complex Data
JS Functions
JS Functions Return
JS Functions Convert Temperature
JS Functions Return Error
JS Function as a variable
JS Function Local Variables
JS Variables
JS Objects part 1
JS Objects part 2
JS Objects part 3
JS Objects part 4
JS Objects part 5
JS Objects part 6
JS Objects part 7
HTML Events part 1
HTML Events part 2
HTML Events part 3