Javascript DOM onclick Event via addEventListener() method

Introduction

In JavaScript, using the addEventListener() method:

object.addEventListener("click",
       myScript);

This example uses the addEventListener() method to attach a "click" event to a p element.

View in separate window

<!DOCTYPE html>
<html>
<body>
<p id="demo">Click me.</p>

<script>
document.getElementById("demo").addEventListener("click", myFunction);

function myFunction() {// www  .  j ava2s . co  m
  document.getElementById("demo").innerHTML = "YOU CLICKED ME!";
}
</script>

</body>
</html>
Bubbles:
Cancelable:
Event type:
Supported HTML tags:












Yes
Yes
MouseEvent
All HTML elements,
EXCEPT:
<base>,
<bdo>,
<br>,
<head>,
<html>,
<iframe>,
<meta>,
<param>,
<script>,
<style>, and
<title>



PreviousNext

Related