0

The question is similar to the one here, except I need to use Javascript. I have a button like so:

<button id="btn1" onclick="clickhandler()">Test</button>

I need this for verification purposes, i.e. to assign the click handler, and then use javascript to verify that the correct function was assigned.

Community
  • 1
  • 1
Freakishly
  • 1,533
  • 5
  • 32
  • 61

3 Answers3

3

UPDATED - totally misunderstood your question...

You want to know if you can verify which handlers are assigned to which tags programmatically... gotcha...

So if you try:

document.getElementById("myButton").onclick.toString();

You'll see that what you get is:

function onclick()
{
    YOUR METHOD NAME HERE
}

I'm sure you can take it from there... everything between "function onclick\n{" and "\n}" is your method.

Sorry I misunderstood your question originally!

B

Brian
  • 2,772
  • 15
  • 12
1

Just do the same, but in pure javascript:

var onclick = document.getElementById("btn1").onclick;
Wulf
  • 3,878
  • 2
  • 22
  • 36
  • 1
    It should be: var onclick = document.getElementById("btn1").onclick; You capitalized the 'd' – Alic Dec 11 '14 at 01:55
0

you could use getAttribute:

var anc = document.getElementById("anc");
alert(anc.getAttribute('onclick'));
Jaime
  • 6,736
  • 1
  • 26
  • 42
  • Tried document.getElementById('anc').getAttribute('onclick')); and it didn't work. I didn't see getAtrribute support the onclick property, so I posted this question :( – Freakishly Jul 26 '11 at 18:58
  • I don't think the getAttribute cares about the name of the attribute you're trying to get.... try this http://jsbin.com/etawel/edit – Jaime Jul 26 '11 at 19:17