JavaScript OnBlur event prevents HTML form from being submitted

Artem picture Artem · Apr 13, 2009 · Viewed 15.7k times · Source

Consider the following form:

<form action="/a" method="post"> 
    <input name="x" type="text" onblur="myonblur(e)" />
    <input name="y" type="text" />
    <input name="submit" type="submit" value="Submit" />
</form>

When focus is on element with name "x" and user presses submit button, onblur event fires but form is NOT submitted. Is there any way to make submit button work as expected if I have onblur events in my form?

Answer

Click Upvote picture Click Upvote · Apr 13, 2009

Consider optimizing your onBlur() method so it takes less time to run. If your form is prevented from getting submitted, it might be because the onBlur() is too long and hangs the browser from doing anything else.

You can also use the onChange() method instead of onBlur(). That may work better for you.