Javascript DOM HTML document addEventListener() Method with anonymous function

Introduction

When passing parameter values, use an "anonymous function" that calls the specified function with the parameters:

Click anywhere in the document to perform a calculation.

View in separate window

<!DOCTYPE html>
<html>
<body>

<p>This example demonstrates how to pass parameter values when using the
addEventListener() method.</p>
<p id="demo"></p>

<script>
var p1 = 5;
var p2 = 7;

document.addEventListener("click", function() {
  myFunction(p1, p2);
});//from  ww  w  .ja va2  s . c  om

function myFunction(a, b) {
  var result = a * b;
  document.getElementById("demo").innerHTML = result;
}
</script>

</body>
</html>



PreviousNext

Related