Element addEventListener() Method - Execute a function when a user clicks on a <button> element: - Javascript DOM

Javascript examples for DOM:Element addEventListener

Description

Element addEventListener() Method - Execute a function when a user clicks on a <button> element:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<button id="myBtn">Test</button>

<p id="demo">

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

function myFunction() {/*from  w w  w .j a  va2 s.c o m*/
    document.getElementById("demo").innerHTML = "Hello World";
}
</script>

</body>
</html>

Related Tutorials