views:

120

answers:

2

I have a page with the following code:

<a id="test" href="someurl" onclick="somefunction">link</a>

I need to actually read the 'onclick' data. As such, I would like something to the effect of

alert(document.getElementById('test').onClick)

Sadly, it returns undefined. What do I need to do to get somefunction?

+4  A: 

Assuming your anchor tag is well-formed, you can get the literal text of that attribute using getAttribute(), e.g.:

alert(document.getElementById('test').getAttribute('onclick'));

Hopefully you aren't using onclick for some unscrupulous purpose like storing data. :3

Faisal
Thanks, this worked. My purposes are lacking only in a few scruples - I'm making a GM script to automate clicks on a site that tries to avoid automating clicks by making links call randomly-generated onclick functions first. This way, I can simply grab the function name, eval() it, and then document.location to the href. :)
Mala
Maybe I shouldn't have asked about scruples, heh. The first thing that comes to mind on reading your description is conning advertising click-throughs, but I doubt you'd use a GM script if you were serious about that. :3 I'll just continue to assume the best...
Faisal
nothing illegal/immoral. Just some annoying "click here to remove this box" crap
Mala
Ah, good fun. :3
Faisal
A: 

You should try:

document.getElementById('test').onclick
Josiah