Teddy
Teddy

Reputation: 18572

Use jQuery to find an event object

jQuery has wonderful selectors for DOM elements, but can you 'select' an event object.

That is, if I use inline JavaScript on the onclick attribute of an element, could I have a selector to find that event object so I can act on it.

I know I could do something like this:

$("a#some_link").click(function(event){//act on event here});

But how would I find the event object if inline JavaScript was used:

<a href="somepage.html" onclick="alert('how do I get my event oject?');">click me</a>

This question is a supplement to an earlier question: calling e.stopImmediatePropagation() from onclick attribute

Upvotes: 0

Views: 1834

Answers (3)

Orbling
Orbling

Reputation: 20612

Well this should find 'onclick' events, I do not believe it picks up the jQuery bound events.

$("a,input").filter(function() {
    return $.isFunction(this.onclick)
});

If there are any other tag types you might have such an event on, then you would need to add them, you can use "*" as the selector, but that is quite slow.

Upvotes: 0

user372551
user372551

Reputation:

onclick = "alert(event.type);return false;"

<a id="some_link" href="somepage.html" onclick="alert(event.type);return false;">click me</a>

Test it

Upvotes: 1

Z. Zlatev
Z. Zlatev

Reputation: 4820

Simple $('a').attr('onclick') gets you that data. In Firefox it's wrapped with function() { }

Upvotes: 1

Related Questions