Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if the button has on-click listener

I am wondering how can I check if the button has on-click listener. My webpage has a button, but sometimes clicking the button has no effect.I am wondering if I can check if the click handler is attached.

I tried this, but it doesn't work..

$('#changeStatus').onClick()
like image 812
chen Avatar asked Dec 01 '25 07:12

chen


1 Answers

It depends on how the listener was applied....

If the listener was set up on the event property, you can check the property:

let input = document.querySelector("input");
input.onclick = function(){ console.log("I was clicked"); }

if(input.onclick){
  console.log("There is a click event listener.");
} else {
  console.log("There is NO click event listener.");
}

if(input.onkeydown){
  console.log("There is a keydown event listener.");
} else {
  console.log("There is NO keydown event listener.");
}
<input>

But, if the listener is registered using the modern DOM API of .addEventListener(), then no, there is no way to know.

like image 103
Scott Marcus Avatar answered Dec 02 '25 21:12

Scott Marcus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!