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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Mike Trpcic
  • 25,305
  • 8
  • 78
  • 114

6 Answers6

51

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

var script = $('#google').attr("onclick")
mkoryak
  • 57,086
  • 61
  • 201
  • 257
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!

Community
  • 1
  • 1
jrharshath
  • 25,975
  • 33
  • 97
  • 127
12

This works for me

 var link_click = $('#google').get(0).attributes.onclick.nodeValue;
 console.log(link_click);
hexdump
  • 143
  • 2
  • 8
  • 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. – JD Smith Nov 03 '14 at 20:28
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.

Alex Gyoshev
  • 11,929
  • 4
  • 44
  • 74
  • $('#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. – Alex Gyoshev Dec 19 '11 at 08:19
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.

Mottie
  • 84,355
  • 30
  • 126
  • 241
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 {
        // ...
    }
});
Tom van Zummeren
  • 9,130
  • 12
  • 52
  • 63