Javascript examples for DOM:Element addEventListener
The addEventListener() method attaches an event handler to the specified element.
Parameter | Description |
---|---|
event | Required. name of the event. |
function | Required. Event handler function |
useCapture | Optional. |
Possible values for useCapture:
No return value
The following code shows how to Attach a click event to a <button> element. When the user clicks on the button, output "Hello World" in a <p> element with id="demo":
<!DOCTYPE html> <html> <body> <button id="myBtn">Test</button> <script> document.getElementById("myBtn").addEventListener("click", function(){ this.style.backgroundColor = "red"; });/* ww w. j a va 2s.c o m*/ </script> </body> </html>