DOM Events with JavaScript

Learn to create webpage interactivity by leveraging JavaScript events in the browser.

Start[missing "" translation]

Key Concepts

Review core concepts you need to learn to master this subject

JS Event Handlers

//Assuming there is an element with ID='test' on the page document.getElementById('test').onclick = function(e) { alert('Element clicked!'); };

The goal of JavaScript is to make a page dynamic, which frequently means responding to certain events (for example, button clicks, user scrolls, etc). DOM elements can have functions hook onto events. The functions are called event handlers and the DOM element is known as an event target.

The example code block shows how to register a function as an event handler. The property name for event handlers starts with 'on' with the event appended afterwards. Examples: onload, onclick, onfocus, onscroll.

Event object

//Assuming there is an element with ID='test' on the page document.getElementById('test').onclick = function(e) { alert('Element clicked!'); };

Event handler functions are passed an argument called an event object, which holds information about the event that was fired.

Event objects store information about the event target, the event type, and associated listeners in properties and methods. For example, if we wanted to know which key was pressed, the event object would store that information.

DOM Events with JavaScript
Lesson 1 of 1
  1. 1

    When you refresh your email inbox, double tap on a post, or scroll through a newsfeed — something cool happens in your browser. These actions are known as events! Events on the web are us...

  2. 2

    After a specific event fires on a specific element in the document object model (or DOM), an event handler function can be created to run as a response. In this lesson, we will learn about _even...

  3. 3

    You're doing great! Now it's time to dive into using event handler functions to create interactivity. JavaScript interprets registered events as event objects with properties and methods. When y...

  4. 4

    It's best practice to create named event handler functions, instead of anonymous functions. Your code will remain organized and reusable this way, even if your code gets complex. Check out the synt...

  5. 5

    The [...] method is used reverse the [...] method. This method stops the code from "listening" for an event to fire when it no longer needs to. [...] also passes two arguments: 1. The event t...

  6. 6

    JavaScript stores events as event objects with their related data and functionality as properties and methods. There are pre-determined properties associated with event objects. You can call thes...

  7. 7

    Beyond the [...] event, there are all types of DOM events that can fire in a browser! It's important to know most events in the DOM take place without being noticed because there are no event h...

  8. 8

    When you click down or move the mouse device mouse events fire. Outside of the [...] event, there are other events that can be fired when using a mouse. The [...] event is fired when the use...

  9. 9

    Another popular type of event is the keyboard event! keyboard events are triggered by user interaction with keyboard keys in the browser. The [...] event is fired while a user presses a key do...

  10. 10

    Congrats, you completed the lesson! Now you've learned about JavaScript events and you can leverage these events on the DOM to create interactivity with event handlers. Let's review what you've l...

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo

DOM Events with JavaScript

Start[missing "" translation]