Javascript DOM HTML Element addEventListener() Method using anonymous function

Introduction

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

This example demonstrates how to pass parameter values when using the addEventListener() method.

Click the button to perform a calculation.</p>

View in separate window

<!DOCTYPE html>
<html>
<body>
<button id="myBtn">Test</button>
<p id="demo"></p>
<script>
var p1 = 5;
var p2 = 7;

document.getElementById("myBtn").addEventListener("click", function() {
  myFunction(p1, p2);
});// ww w. ja  va  2s  . c  o  m

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

</body>
</html>



PreviousNext

Related