简体   繁体   中英

How do I get the value of onclick from a button using Javascript

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, ie to assign the click handler, and then use javascript to verify that the correct function was assigned.

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

只需执行相同操作,但使用纯JavaScript:

var onclick = document.getElementById("btn1").onclick;

you could use getAttribute:

var anc = document.getElementById("anc");
alert(anc.getAttribute('onclick'));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM