Javascript Addeventlistener
-
The event target method addEventListener() sets up a function that will be called whenever the specified event is delivered to target.
-
Common targets are Elements, Document, and Window, but the target may be any object that supports events (such as XML Https request).
-
addEventListener() works by adding a function or an object that implements event listeners for the specified event type on the event target on which it is called.
The syntax of addEventListener() is as follows:
document.addEventListener("click", function(){
document.getElementById("demo").innerHTML = "Hello World";
});
Example: Program to add a click event to the document:
Output
After Clicking