It’s Festive Season! Enjoy 30% Off

Use code FESTIVE30 at checkout

Code has been added to clipboard!

How to Create JavaScript Events: Master JavaScript Event Handlers Now

Reading time 3 min
Published Aug 8, 2017
Updated Oct 1, 2019

JavaScript events are specific actions or proceedings that happen in the program or website you are developing. You can set up a response which will be triggered by specified events. For example, JavaScript mouse events refer to various cursor movements over elements.

In this tutorial, you'll learn what JavaScript event handlers are, how to create JavaScript events, and how to add JavaScript button events and browser events. You'll also grasp how, why, and where you can add events with JavaScript to increase the quality of your site, make it more polished and user-friendly.

JavaScript Events: Main Tips

  • Anything that happens to an HTML element using JavaScript is called a JavaScript event.
  • JavaScript can execute some specific code when a particular event happens.
  • A JavaScript event can be something that a user does (e.g., clicking on an HTML element) or something that a browser does (e.g., finishing loading a page).

Events: What and When?

Keep in mind that HTML event attributes can run JavaScript directly or through a function. The functions can and should be created by you. This way they will suit your needs best.

You can also prevent JavaScript events from being handled. JavaScript event handlers can be useful when you want to execute a particular functionality when something changes on the web page.

These are the most common web page events:

  • A page loads
  • User clicks a button
  • User inputs data

Event Handler Attributes

JavaScript event handler attributes can be inserted into HTML elements. In the example below, you can see one of the simplest JavaScript button events. A JavaScript event handler attribute onclick is added to an HTML button element.

When the user clicks the button, a JavaScript event occurs. Also, the code defines what to do when that event occurs:

Example
<button onclick="document.getElementById('dateOut').innerHTML = Date()">What is the time</button>

Usually, the code to be executed on the event is longer than one line. In that case, it is more convenient to use functions:

Example
<button onclick="myFunction()">What is the time</button>

Common Events

There are plenty of possible JavaScript events to make your website dynamic and neat. However, as in every programming language, some functions are more popular than others because they are more convenient and suitable within context. For instance, JavaScript mouse events include cases when the cursor hovers over elements, is clicked or moved over an image.

Here is a list of the most common JavaScript event handlers (tools that provoke a JavaScript function to run):

JavaScript Event Handlers Description
onchange Occurs when an HTML element changes
onclick Occurs when the user clicks on an HTML element
onmouseover Occurs when the user hovers their cursor over an HTML element
onmouseout Occurs when the user hovers their cursor away from an HTML element
onkeydown Occurs when the user presses a keyboard key
onload Occurs when the current web page finishes loading

JavaScript Events: Summary

  • JavaScript events occur when a JS code is executed because of some action that occured.
  • JavaScript event handlers are implemented into HTML elements. You can read more about the in our HTML Elements tutorial.
  • The actions triggering events in JavaScript are done by either the user or the browser.