Document addEventListener() Method - Javascript DOM

Javascript examples for DOM:Document addEventListener

Description

The document.addEventListener() method attaches an event handler to the document.

Syntax

document.addEventListener(event, function, useCapture)

Parameter Values

Parameter Description
event Required. name of the event.
function Required. function to run when the event occurs.
useCapture Optional. whether the event should be executed in the capturing or in the bubbling phase.

Possible values for useCapture:

  • true - The event handler is executed in the capturing phase
  • false - Default. The event handler is executed in the bubbling phase

Return Value:

No return value

The following code shows how to Attach a click event to the document.

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<script>
document.addEventListener("click", function(){
    document.body.style.backgroundColor = "red";
});//  w  ww  . ja v a 2 s .c o m
</script>

</body>
</html>

Related Tutorials