3

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

1
  • In case someone is wondering, my browser is Chrome 17. Commented Feb 13, 2012 at 23:06

3 Answers 3

5

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

Sign up to request clarification or add additional context in comments.

Comments

4

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

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.