Ignaciovl Ignaciovl - 9 days ago 6
Javascript Question

onclick confirm not being prompted

I have the following in one of my .jsp files:

...
<input style = "margin-left: 100px" type="submit" name = "cancel" onclick="return confirm('Are you sure?');" value="Cancel" />
...


The above works perfectly, when I click on the submit, a window pops with the corresponding confirmation message. On the other hand, in another .jsp file I have the following (which is nearly the same as above):

...
<input style = "margin-left: 100px" type="submit" name = "confirm" onclick="return confirm('Proceed to results?');" value="Confirm" />
...


In this case, the prompt window does not appear at all, and when I press on the submit it loads the next page without asking for a confirmation. What is the difference that makes the second example not work as intended?

Answer

This happens because you gave your input a name of confirm. Some browsers like Chrome will put named form elements on the global scope (window). And since your element is named confirm it overwrites window.confirm function to be your element.

Simply name your input something else

<input type="submit" name="myconfirm" onclick="return confirm('Proceed to results?');" value="Confirm" />