Which method is used to attach an event listener to an element in JavaScript?
- addListener()
- addEventListener()
- attachEvent()
- addEventHandler()
In JavaScript, the correct method to attach an event listener to an element is addEventListener(). This method allows you to specify the event type to listen for and the function to execute when the event occurs. It is the modern and recommended way to handle events in JavaScript.
Loading...
Related Quiz
- Given the short-circuiting nature of logical operators in JavaScript, what will be the output of the expression false && someUndeclaredVariable?
- What does a Promise represent in JavaScript?
- What is a prototype in JavaScript?
- In a UI with nested dropdown menus, a developer wants to ensure that clicking a nested menu item does not trigger the click event of its parent menu. What method can be used to stop the event from reaching the parent menu?
- Which of the following is a way to create a singleton object in JavaScript?