Skip to content Skip to sidebar Skip to footer

Javascript Onchange Event Preventing Onsubmit Event In Html Form?

Consider an HTML form:

Solution 1:

Solution (of a sort):

It turns out that it is only presence of an alert() - or a confirm() - during the input's onchange event that causes the form's onsubmit event to not fire. The JS thread appears to get blocked by the alert(). The workaround is to not include any alert() or confirm() in the input's onchange call.

Browsers known to be affected: IE6 - Windows IE8 - Win FF3 - Win

Browsers known not to be affected: Google Chrome - Win Safari - Mac FF - Mac

Solution 2:

I tried with Firefox 3 (Mac) with the following code:

<html><head><script>functionvalidate(ele)
                {
                    alert("vali");
                    returnfalse;
                }

                functioncheckValidArray()
                {
                    alert("checkValidArray");
                }

            </script></head><body><formaction=""method="POST"onsubmit="return checkValidArray()"><inputtype="text"name="data"onchange="return validate(this);"><inputtype="submit"value="Ok"></form></body></html>

It seems to work. When I click on the Ok button, both "Vali" and "Check Valid Array" pop up. Initially I thought return false could be the reason why the form was not submitted, but it IS submitted (at least, checkValidArray() is called).

Now, what are you doing in your checkValidArray() and validate() methods? Something special? Can you post the code?

EDIT: I tested with IE8 and FF3 on windows, and here both events do not get fired. I have no idea why. Perhaps onblur IS a solution, but WHY does onchange not work? Is there a specific reason or is it just another inconsistency? (Works on both FF and Safari on Mac)

Solution 3:

You could use the onblur event in your inputs instead of onchange.

Solution 4:

It's quite interesting that the behaviour is different on the Mac, so it appears platform dependent and not browser dependent. Must be a clue there somewhere...

I tried onblur with Nivas' code but had the same result as with onchange (only the "Vali" alert).

What does make a difference is whether you do anything in validate(). Comment out the line alert("vali"); and it works! (The actual return value from validate() does not matter, although I wouldn't expect it to.)

EDIT: A colleague just tried this in Google Chrome on Windows and it works there. This intuitively makes sense because of how Chrome separates JS threads.

Something about the first alert() blocking the thread causes the onsubmit event to get lost. Possible race condition?

Solution 5:

Don't create ; submit button. Instead, create a normal button and write a function which can be called onClick with that button.

Function will do the validation on the form fields, and if everything is fine, it will submit the form. Otherwise it will not.

function validate
{
  "Piece of code to Validate your form"document.formName.action.value = "URL which you want to call"document.propFile.submit(); // It will submit he page
}

Post a Comment for "Javascript Onchange Event Preventing Onsubmit Event In Html Form?"