Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. :)

like image 696
Web_Designer Avatar asked Apr 20 '26 21:04

Web_Designer


2 Answers

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

  • http://jsfiddle.net/pFYUM/1/
like image 127
calebds Avatar answered Apr 23 '26 10:04

calebds


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

like image 39
ori Avatar answered Apr 23 '26 11:04

ori