Events in JavaScript are actions that happen inside the HTML DOM when a user does something. When such events happen, we can use JavaScript to react to it.
Writing code in your website that reacts to the events makes your website highly interactive. We will see why in this and upcoming lessons.
Some Examples of Events in the DOM:
- When a user clicks on something.
- When the document is loaded.
- When a user types into an input field in a form.
- When a user presses a key in their keyboard.
- When a user hovers their mouse pointer over an HTML element.
Listening to the above events and then performing some actions can make your website intuitive. Just let your imagination run wild 😎.
Note: In this lesson, we will only go through one or two examples from each event type. But don’t worry, as you can see in the left side menu, we have a separate chapter (JS DOM Events) to explain all of them in detail.
Note: Also check out our JavaScript 👉 Add and Delete Elements and 👉 Add or Change CSS Styles lesson.
HTML DOM Event Types
We have categorized some of the most loved and used DOM Events into five categories. They are as follows:
Note: The Document Object Model (DOM) provides lots of events and you don’t need to learn every single one of them as there are so many. We have shortlisted some of the most important ones.
JavaScript Window Events 🪟
Window Events are actions that happen to the browser window when a user does something that affects the entire browser window. A window contains the entire DOM document.
Some of the most popular ones are given below:
- onload event
- onscroll event
- onresize event
- onhashchange event
JavaScript Mouse Events 🖱
Mouse Events are actions that happen when a user does something with their mouse. A user clicking, double-clicking, hovering the mouse over an HTML element, etc. are some of the mouse events that occur in the DOM.
Some of the most popular ones are given below:
- onclick event
- ondblclick event
- onmouseover event
- onmouseout event
- onmouseup event
- onmousedown event
- onmousemove event
JavaScript Keyboard Events ⌨
Keyboard Events are actions that happen when a user does something with their keyboard. A user pressing a key once, keep pressing a key, etc. are some of the keyboard events that occur in the DOM.
Some of the most popular ones are given below:
- onkeydown event
- onkeypress event
- onkeyup event
JavaScript Form Events 📃
Form Events are actions that happen when a user does something with the input fields or the entire HTML form element. A user typing inside an input field, submitting or resetting form, etc. are some of the form events that occur in the DOM.
Some of the most popular ones are given below:
- onchange event
- onsubmit event
- onreset event
- onfocus event
- onblur event
- onselect event
JavaScript Drag Events 💪
Drag Events are actions that happen when a user drags and/or drops something (mostly an HTML element) in the HTML DOM. A user dragging, dropping an HTML element from one place to another, etc. are some of the drag events that occur in the DOM.
Some of the most popular ones are given below:
- ondrag event
- ondragenter event
- ondragleave event
- ondragover event
- ondrop event
- ondragstart event
- ondragend event
In the next lesson we will be reacting to these events and performing actions based on the events occurred using JavaScript. Stay tuned 😉.