0

I just bumped into a very strange javascript error.

I have a form with a button input field:

<input type="button" id="flagAnswer" value="Flag" onclick="flagAnswer()">

In my js file that's included in the page I have defined the flagAnswer function:

function flagAnswer(){
   // do stuff
}

When I try clicking the flag button, however, I get the following error:

TypeError: flagAnswer is not a function

I've been fiddling around with this as it seems extremely straightforward, and the trouble seems to be that the id of the input field and the name of the function are identical. If I change the function name slightly, or if I change the input ID, it works.

Why is this happening? Any way to avoid it other than renaming one of them?

Edit:

jsfiddles to illustrate my point:

Original with error (click doesn't work) - http://jsfiddle.net/aQ9zt/1/

Changing function name (click works) - http://jsfiddle.net/r8m9L/4/

Changing input ID (click works) - http://jsfiddle.net/r8m9L/5/

9
  • flagTerminal? flagAnswer? Which is it? Commented Nov 18, 2013 at 9:53
  • @deceze - whoops, sorry, updated Commented Nov 18, 2013 at 9:55
  • 1
    function implementation flagAnswer is bad syntax [no brackets] Commented Nov 18, 2013 at 9:56
  • 1
    can we see your code? Commented Nov 18, 2013 at 10:02
  • 3
    see this SO post Commented Nov 18, 2013 at 10:20

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.