0

Given an element, is there any way to tell with Javascript/jQuery whether there are any click events on that element?

I mean ANY click events. If clicking it would cause ANY code to execute, I want to know about it.

Is this possible?

It must be, if Firebug and Chrome Dev Tools can see all the event listeners tied to an object...

temporary_user_name
  • 35,956
  • 47
  • 141
  • 220

3 Answers3

1

To get the list of events attached to particular element jquery uses

​$._data( $("#yourelement")[0], "events" );
Prasanna Aarthi
  • 3,439
  • 4
  • 26
  • 48
  • @zerkms Replace your fiddle code with the correct one $('div').on('click', 'span', function() { alert('handler'); }); console.log($._data( $("#hello")[0], "events" )); and your HTML as
    click me
    Im able to view the results in console
    – Prasanna Aarthi Aug 28 '13 at 06:00
  • the click handler is delegated to `span`, not to `div`. So your change is not correct. Put `id="hello"` to `span` and see it doesn't work – zerkms Aug 28 '13 at 06:01
0

There is a script called VisualEvent that does just the work you want. You can look at its code and see what goes on there.

0

See http://jsfiddle.net/jvcDV/

HTML:

<a id="p1">I am link 1</a>
<a onclick="javascript:alert('hi');">I am link 2</a>
<a>I am link 3</a>
<a>I am link 4</a>

JS:

$(document).ready(function()
{
    // Add inline event to a link element
    document.getElementById('p1').onclick = function() { alert('hi'); };
    $('a').each(function()
    {
        if(this.onclick == null)
        {
            alert("Link " + this.innerText + " does not have a onclick event");
        }
        else
        {
            alert("Link " + this.innerText + " does have a onclick event");
        }
    });             
});
andrewb
  • 2,995
  • 7
  • 54
  • 95