DOM Events

DOM (Document Object Model) events are actions that occur in the web browser, often triggered by user actions such as clicking a button, moving the mouse, or typing on the keyboard. Handling these events allows you to create dynamic and interactive web applications.

Types of DOM Events

There are various types of DOM events, including but not limited to:

Handling Events

You can handle events in JavaScript by using event listeners. The addEventListener method allows you to add event listeners to DOM elements.

Example: Click Event
Try yourself
        
            document.getElementById("myButton").addEventListener("click", function() {
    alert("Button was clicked!");
});
        
    

In this example, when you click the button, an alert will be displayed.


Example: Mouseover Event
Try yourself
        
            document.getElementById("myText").addEventListener("mouseover", function() {
    this.style.color = "red";
});
        
    

In this example, when you hover over the text, its color changes.


Example: Keydown Event
Try yourself
        
            document.addEventListener("keydown", function(event) {
    document.getElementById("output").textContent = "Key Code: " + event.keyCode;
});
        
    

In this example, when you press any key, the key code is displayed on the screen.


Example: Form Submit Event
Try yourself
        
            document.getElementById("myForm").addEventListener("submit", function(event) {
    event.preventDefault();
    alert("Form submitted!");
});
        
    

In this example, when you submit the form, an alert will be displayed with the form data.


Event Object

When an event occurs, an event object is created. This object contains properties and methods that you can use to get information about the event and manipulate it. Common properties include:

Event Bubbling and Capturing

When an event occurs, it goes through a process called event propagation. This process consists of three phases:

By default, event listeners are triggered during the bubbling phase. You can specify whether the event should be captured or bubbled by passing a third argument to addEventListener.

Example: Event Propagation

Try yourself
        
             document.getElementById("outer").addEventListener("click", function() {
     alert("Outer div clicked (capturing)");
 }, true);

 document.getElementById("inner").addEventListener("click", function() {
     alert("Inner div clicked (bubbling)");
 });
        
    

In this example, both capturing and bubbling phases are demonstrated.


Stopping Event Propagation

You can stop the propagation of an event using the stopPropagation method. This can be useful when you want to prevent an event from being handled by parent elements.

Example: Stop Propagation
Try yourself
        
            document.getElementById("inner").addEventListener("click", function(event) {
    alert("Inner div clicked");
    event.stopPropagation();
});

document.getElementById("outer").addEventListener("click", function() {
    alert("Outer div clicked");
});
        
    

In this example, the event propagation is stopped when clicking on the inner element.


Preventing Default Actions

The preventDefault method cancels the event if it is cancelable. For example, you can prevent a form from being submitted or a link from being followed.

Example: Prevent Default
Try yourself
        
            document.getElementById("myLink").addEventListener("click", function(event) {
    event.preventDefault();
    alert("Default action prevented");
});
        
    

In this example, the default action of the link is prevented.


Important Notes

Here are some important notes and best practices for handling DOM events: