Javascript DOM HTML Element getAttribute() Method get onclick event attribute of <button>

Introduction

Get the value of the onclick event attribute of a <button> element:

var x = document.getElementById("myBtn").getAttribute("onclick");

Click the button to display the value of the onclick attribute of the button element.

View in separate window

<!DOCTYPE html>
<html>
<body>

<button id="myBtn" onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {/*from   w w w .  java  2 s  . c  om*/
  var x = document.getElementById("myBtn").getAttribute("onclick");
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>



PreviousNext

Related