The addEventListener() method is used to attach an event handler to a particular element. It does not override the existing event handlers. Events are said to be an essential part of the JavaScript. A web page responds according to the event that occurred. Events can be user-generated or generated by API's. An event listener is a JavaScript's procedure that waits for the occurrence of an event.
The addEventListener() method is an inbuilt function of JavaScript. We can add multiple event handlers to a particular element without overwriting the existing event handlers.
Example
To see the effect when we click on the button.
Output:
After clicking on the button, the output will be-
Example
Multiple events of a different type to the same element.
Output:
When we hover on the button, the output will be-
After clicking on the button, the output will be-
Proper code: