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.
Contents
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
- Easy to use with a learn-by-doing approach
- Offers quality content
- Gamified in-browser coding experience
- The price matches the quality
- Suitable for learners ranging from beginner to advanced
- Free certificates of completion
- Focused on data science skills
- Flexible learning timetable
- Simplistic design (no unnecessary information)
- High-quality courses (even the free ones)
- Variety of features
- Nanodegree programs
- Suitable for enterprises
- Paid Certificates of completion
- A wide range of learning programs
- University-level courses
- Easy to navigate
- Verified certificates
- Free learning track available
- University-level courses
- Suitable for enterprises
- Verified certificates of completion
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:
<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:
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.