'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
Solution 1:[1]
onclick = "alert(event.type);return false;"
<a id="some_link" href="somepage.html" onclick="alert(event.type);return false;">click me</a>
Solution 2:[2]
Simple $('a').attr('onclick')
gets you that data. In Firefox it's wrapped with function() { }
Solution 3:[3]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Z. Zlatev |
Solution 3 | Orbling |