I found the way to check if event exists on element. But it's not work on the events which is not delegated by jQuery...
When I try,
$("a").data("events");
for this.
<a href="#" onClick="alert('Hello, World!')" />
It returned undefined.
Is there any way to check if onClick exists on elements with jQuery?
Thanks.
To check if an element was clicked, add a click event listener to the element, e.g. button. addEventListener('click', function handleClick() {}) . The click event is dispatched every time the element is clicked. Here is the HTML for the examples in this article.
$( "#myDiv" ). show();
Use the off() method to override jQuery event handlers. This method is used to remove an event handler.
You can do:
if ($('a').attr("onClick") != undefined) {}
Just do:
if( myelement.onclick != null )
{
//onclick exists
}
else
{
//onclick doesn't exist
}
No need for jQuery.
if( $('#elementName').click == undefined)
{ //event handler doesn't exist }
else
{ //handler exists }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With