简体   繁体   中英

javascript - call function when button is clicked

Can someone show me what I am doing wrong here?

I am trying to execute a function when a button is clicked.

The HTML:

<button id="btn1">Press me!</button>
<input type="button" id="btn2" value="Press me!"/>​

The javascript:

var btn1 = document.getElementById('btn1'),
    btn2 = document.getElementById('btn2');


function do() {
    alert('Yay!');
}
btn1.onclick = do;
btn2.onclick = do;​

Here's a demo. I'm not sure why it's not working. It's probobly something way to obvious for me to notice. :)

Change the name of your function from do to something that is not a reserved keyword.

do is a keyword. If you used a debugger you'd know that.

do is a keyword. Change variable name and it will work.

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