bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/JavaScript/DOM and Browser APIs
JavaScript•DOM and Browser APIs

JavaScript Events

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind JavaScript Events?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

___ JavaScript
3Order

Put the learning moves in the order that makes the concept easiest to apply.

JavaScript Event Handlers
Common HTML Events
Calling a JavaScript Function

HTML Events

HTML events are things that happen to HTML elements.

Examples of events

  • An HTML button is clicked
  • A web page has finished loading
  • The mouse moves over an element
  • A keyboard key is pressed
  • An HTML input field is changed

HTML DOM allows JavaScript to react to HTML events:

JavaScript Events

Often, when events happen, you may want to do something.

When JavaScript is used in HTML pages, JavaScript can react on events .

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:

Example

<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>

Calling a JavaScript Function

JavaScript code can often be several lines long.

It is more common to use the event attribute to call a function:

Example

<button onclick="displayDate()">The time is?</button> <script> function displayDate() {
  document.getElementById("demo").innerHTML = Date();
}
</script>

Common HTML Events

Here is a list of some common HTML events:

EventDescription
onchangeAn HTML element has been changed
onclickThe user clicks an HTML element
onmouseoverThe user moves the mouse over an HTML element
onmouseoutThe user moves the mouse away from an HTML element
onkeydownThe user pushes a keyboard key
onloadThe browser has finished loading the page

JavaScript Event Handlers

An event handler is JavaScript code that runs when an event happens.

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 ...

Using an Event Listener

Using event attributes like onclick are easy to use.

Nevertheless, using addEventListener() is the recommended way to handle events.

onclick

The addEventListener() method keeps HTML and JavaScript separated.

addEventListener()

You will learn a lot more about events and event handlers in the HTML DOM chapters.

Previous

HTML DOM Document

Next

JavaScript Mouse Events