onsubmit event not working if in the form is a input with confirm name or id .
if i add below input to the form the onsubmit event not working
<input type="checkbox" name="confirm" id="confirm">
<form action="/" method="post" onsubmit="return confirm('Are you sure ?')">
<input type="text" name="name">
<br>
<input type="checkbox" name="confirm" id="confirm">
<button type="submit">submit</button>
</form>
when i removed the input with confirm name the onsubmit event work!!!
it is very strange and unusual.
4
Answers
Avoid using reserved words or built-in function names for form element names or IDs.
This issue occurs b/c confirm is a built_in JS function that shows a dialog box with OK & cancel buttons. When a form input is named or has an ID of "confirm", it conflicts with this function, which causes the onsubmit event to fail.
Rename the checkbox to something like confirmCheck.
The confirmation dialog for the form’s onsubmit property is probably interfering with the form submission process, which is causing the issue you’re seeing. In particular, the form submission will be stopped if the user does not confirm the dialog. This can be problematic if you anticipate specific actions to be performed independent of the user’s approval.
Here’s a summary of how you can deal with this better:
Ensure Checkbox-Based Form Submission: You must check the checkbox’s state in the Java Script function if you want to make sure the form only submits when the user confirms the dialog and the checkbox is checked.
JavaScript Validation: Change the onsubmit function to first ask for confirmation after determining whether the checkbox is checked.
This is the most recent version of your form:
Within this code:
ticked.
the checkbox is not checked.
user cannot submit the form if they select "Cancel" in the
confirmation popup.
This guarantees that the form submission will only continue if the user approves the action and the checkbox is checked.
Your issue is that the scope of
onXYZ
attribute handlers (DOM level 0 handlers) is "augmented" in a way that adds the form’s identifiers to the scope of your script specified in the attribute, and soconfirm
ends up referring to your input element, not the browser’sconfirm
method (window.confirm
).To do this, the browser creates an ad-hoc function and puts the content of your attribute into this function (
return confirm('Are you sure ?')
), which gets executed when the event (in your case onsubmit) is fired, however, most browsers will change the scope chain of this function so that it’s populated with properties from thedocument
, theform
and the current element. So youronsubmit
code is wrapped in a function more or less that looks like this (see reference):Since the scope of your
onsubmit
method is modified to have bindings for the properties of your form, accessingconfirm
(orname
) accesses the DOM input elements, not the globalconfirm
method. The easiest fix for this is to accesswindow.confirm
, or change your formname
andid
so it doesn’t conflict with a global:But the more appropriate fix would be to avoid using DOM level 0 attribute event handlers, and instead, use
addEventListener()
withe.preventDefault()
to stop the form from submitting.The issue occurs because the confirm() function in your onsubmit event is being overridden by the confirm input element. When you have an element with an id, it creates a global variable with that name, which can interfere with built-in JavaScript functions.
To fix this, you have a few options:
Any of these solutions should resolve the issue and allow your form submission confirmation to work properly.