Javascript DOM HTML Element addEventListener() Method add click event

Introduction

Change the background color of a <button> element:

Click the button to change its background color.

View in separate window

<!DOCTYPE html>
<html>
<body>
<button id="myBtn">Test</button>

<script>
document.getElementById("myBtn").addEventListener("click", function(){
  this.style.backgroundColor = "red";
});/*from w  ww.ja  va  2s.  c om*/
</script>

</body>
</html>



PreviousNext

Related