temporary_user_name
temporary_user_name

Reputation: 37058

Is there a way to tell programmatically if clicking an element will have any effect?

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...

Upvotes: 0

Views: 95

Answers (3)

andrewb
andrewb

Reputation: 3095

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");
        }
    });             
});

Upvotes: 0

Prasanna Aarthi
Prasanna Aarthi

Reputation: 3453

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

​$._data( $("#yourelement")[0], "events" );

Upvotes: 1

user2724066
user2724066

Reputation: 1

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.

Upvotes: 0

Related Questions