Saturday, October 18, 2025
HomeLanguagesJavascriptEvent Firing in JavaScript

Event Firing in JavaScript

What are Events?

Events are actions or occurrences that happen in the system where we do programming, which the system tells you about so that we can respond to these events in some way if desired. For example, if the user selects a button on a webpage, one might want to respond to that action by displaying a dialog box.

Understanding the basics of Events:

HTML




<!DOCTYPE html>
<html lang="en">
  
<body>
    <button class="first">Button 1</button>
    <button class="second">Button 2</button>
    <button class="third">Button 3</button>
  
    <script>
        const firstBtn = document.querySelector(".first");
        const secondBtn = document.querySelector(".second");
        const thirdBtn = document.querySelector(".third");
  
        firstBtn.addEventListener('click', ()=>{
            console.log("Button 1 is Clicked");
        });
        secondBtn.addEventListener('click', ()=>{
            console.log("Button 2 is Clicked");
        });
  
        thirdBtn.addEventListener('click', ()=>{
            console.log("Button 3 is Clicked");
        });
  
    </script>
</body>
  
</html>


Output:

Button 1 is Clicked
Button 2 is Clicked
Button 3 is Clicked

When button 1 is clicked, we get the output in the console as Button 1 is Clicked and similarly depending on the button which is clicked. Here we have an event handler that is associated with the button which looks for the click event.

In JavaScript, we have an event object which has all the information associated with the event which can be used to find out which event is being fired. Consider the example above and now we make the following modifications.

HTML




<!DOCTYPE html>
<html lang="en">
  
<body>
    <button class="first">Button 1</button>
    <button class="second">Button 2</button>
    <button class="third">Button 3</button>
  
    <script>
        const firstBtn = document.querySelector(".first");
        const secondBtn = document.querySelector(".second");
        const thirdBtn = document.querySelector(".third");
  
        firstBtn.addEventListener('click', (e)=>{
            console.log(e.target);
        });
  
        secondBtn.addEventListener('click', (e)=>{
            console.log(e.target);
        });
  
        thirdBtn.addEventListener('click', (e)=>{
            console.log(e.target);
        });
    </script>
</body>
</html>


Output:

Button 1
Button 2
Button 3

We have passed in the event object as a parameter to the function which is to be executed when the event is fired. The target attribute tells us the element that fires the event hence we get our desired answer. This is how we can use the event object in JavaScript to know which element is fired or what type of element is being fired.

If we replace the e.target with just e we would get the event object as the output that can be used to extract any information about the event.

HTML




<!DOCTYPE html>
<html lang="en">
  
<body>
    <button class="first">Button 1</button>
    <button class="second">Button 2</button>
    <button class="third">Button 3</button>
  
    <script>
        const firstBtn = document.querySelector(".first");
        const secondBtn = document.querySelector(".second");
        const thirdBtn = document.querySelector(".third");
  
        firstBtn.addEventListener('click', (e)=>{
            console.log(e.type);
        });
  
        secondBtn.addEventListener('click', (e)=>{
            console.log(e.type);
        });
  
        thirdBtn.addEventListener('click', (e)=>{
            console.log(e.type);
        });
    </script>
</body>
  
</html>


Output:

click
click
click
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS