WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

JavaScript · Basic · question 15 of 100

What are event listeners, and how do you use them in JavaScript?

📕 Buy this interview preparation book: 100 JavaScript questions & answers — PDF + EPUB for $5

In JavaScript, an event listener is a function that is attached to an HTML element and executed when a specific event occurs, such as a click, a mouseover, or a keypress. Event listeners are an essential part of building interactive and dynamic web pages, as they allow you to respond to user actions and trigger actions in your code.

To use an event listener in JavaScript, you must first select the HTML element that you want to attach the listener to, using one of the DOM selection methods such as getElementById or querySelector. Once you have selected the element, you can use the addEventListener method to attach the listener.

The addEventListener method takes two arguments: the type of event to listen for, and the function that will be executed when the event occurs. Here is an example of how to use addEventListener to listen for a click event on a button:

    var button = document.getElementById('my-button');
    
    button.addEventListener('click', function() {
        // Code to be executed when the button is clicked
    });

In this example, the getElementById method is used to select the button element with the ID of ’my-button’. The addEventListener method is then used to attach a click event listener to the button, which executes an anonymous function when the button is clicked.

You can also attach event listeners to multiple elements at once by using a loop to iterate over the selected elements. For example, to attach a click event listener to all buttons on a page:

    var buttons = document.querySelectorAll('button');
    
    for (var i = 0; i < buttons.length; i++) {
        buttons[i].addEventListener('click', function() {
            // Code to be executed when a button is clicked
        });
    }

In this example, the querySelectorAll method is used to select all button elements on the page. A for loop is then used to iterate over the selected elements and attach a click event listener to each button.

Event listeners can also be removed using the removeEventListener method. This method takes the same arguments as addEventListener and removes the specified listener from the element.

In summary, event listeners are functions that are attached to HTML elements and executed when a specific event occurs. To use an event listener in JavaScript, you must first select the HTML element using one of the DOM selection methods and then use the addEventListener method to attach the listener. Event listeners are an essential part of building interactive and dynamic web pages, and their flexibility and versatility make them a powerful tool for web developers.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic JavaScript interview — then scores it.
📞 Practice JavaScript — free 15 min
📕 Buy this interview preparation book: 100 JavaScript questions & answers — PDF + EPUB for $5

All 100 JavaScript questions · All topics