I have the following snippet of code in my jsp page:
<form name="programarRutasForm" method="post" action="/SGT_Galp_web/programarRutas.do">
<table>
<tr>
<td>
<input type="button" name="insereVuelta" value="Insere Vuelta" onclick="insereVuelta()" id="insereVuelta">
</td>
<td>
<input type="submit" name="criaRuta" value="Cria Ruta" id="criaRuta">
</td>
</tr>
</table>
...
When I click the button insereVuelta
, it throws an exception with the message:
Uncaught TypeError: object is not a function
But if I put the button outside the form it works normally. How is this happening? I really need to put the button near the submit button inside the form.
The Error is because of problems with Semi-colons. Check if your script has lines that are missing semi-colons. You would ask "then why does it work outside the form?" The Automatic-Semicolon insertion fails in certain situations; you can read about it more here: What are the rules for JavaScript's automatic semicolon insertion (ASI)?
I feel this would solve the problem.
I believe the problem stems from the function having the same name as the button itself. I had the same problem and changing the name of the button fixed it. It looks like changing the name of your button fixed it for you, as well.
Conjecture:
I would guess that when the names are the same, the interpreter is trying to call the button itself, rather than the function. Because the button isn't a function, it throws the error.
I don't have an explanation for why it worked for you when outside the form, though. Perhaps when a button is outside the form, the interpreter doesn't automatically wire it up as an accessible object in the same way, or maybe it would have to be accessed via a "parent".
insereVuelta is used as the name and id of the input and the name of the function, try changing name and id or the function name.