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/