Get the value of "onclick" with jQuery?
Asked Answered
D

6

31

Is it possible to get the current value of the onClick attribute of an A tag via jQuery?

For example, I have:

<a href="http://www.google.com" id="google" onclick="alert('hello')">Click</a>

I want to get the onclick code so I can store it for later use (as I'll be changing the onclick event for a little while).

Is it possible to do something like:

var link_click = $("#google").onclick;

or:

var link_click = $("#google").click;

So that later on in my code I can re-apply that code, or eval() it if necessary?

Diclinous answered 12/9, 2009 at 17:8 Comment(0)
B
51

i have never done this, but it would be done like this:

var script = $('#google').attr("onclick")
Baucis answered 12/9, 2009 at 17:9 Comment(2)
You can get the onclick attribute. But not always the click event handler.Low
Instead use $('#google')[0].attributes.onclick.nodeValue; Otherwise yours returns function.Miscellany
N
14

mkoryak is correct.

But, if events are bound to that DOM node using more modern methods (not using onclick), then this method will fail.

If that is what you really want, check out this question, and its accepted answer.

Cheers!


I read your question again.
I'd like to tell you this: don't use onclick, onkeypress and the likes to bind events.

Using better methods like addEventListener() will enable you to:

  1. Add more than one event handler to a particular event
  2. remove some listeners selectively

Instead of actually using addEventListener(), you could use jQuery wrappers like $('selector').click().

Cheers again!

Noose answered 12/9, 2009 at 17:16 Comment(0)
B
13

This works for me

 var link_click = $('#google').get(0).attributes.onclick.nodeValue;
 console.log(link_click);
Blindfish answered 10/3, 2013 at 15:30 Comment(1)
This is the answer to go with if you actually need to access the text of the onclick attribute and not the bound functions or events themselves. But, you should replace .get(0).attributes.onclick.nodeValue with .get(0).attributes.onclick.value since nodeValue is being deprecated.Pentomic
H
11
$('#google').attr('onclick') + ""

However, Firebug shows that this returns a function 'onclick'. You can call the function later on using the following approach:

(new Function ($('#google').attr('onclick') + ';onclick();'))()

... or use a RegEx to strip the function and get only the statements within it.

Haymo answered 12/9, 2009 at 17:16 Comment(1)
$('#google').attr('onclick') holds the value of the onclick attribute (which is a string). However, because the browsers treat the event attributes in a different way, they are returned with "function onclick() { alert('hello') }". Thus, if you want to directly call the function, you'll have to either strip the function() {} or call it immediately.Haymo
S
4

I'm not quite sure how to do this in jQuery... but this works:

var x = document.getElementById('google').attributes;
for (var i in x) {
 if (x[i].name == "onclick") alert(x[i].firstChild.data);
}

but like Harshath said it would be better if you used event listeners, as removing and adding this function back into the onclick event may be troublesome.

Spanner answered 12/9, 2009 at 23:39 Comment(0)
R
0

Could you explain what exactly you try to accomplish? In general you NEVER have to get the onclick attribute from HTML elements. Also you should not specify the onclick on the element itself. Instead set the onclick dynamically using JQuery.

But as far as I understand you, you try to switch between two different onclick functions. What may be better is to implement your onclick function in such a way that it can handle both situations.

$("#google").click(function() {
    if (situation) {
        // ...
    } else {
        // ...
    }
});
Retreat answered 12/9, 2009 at 17:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.