Michael Michael - 4 months ago 13x
HTML Question

Chrome form.submit() handler ignores required fields

I expected that calling

on a
with javascript would call the validation functions on each
, just like clicking a submit button.

Am I doing something wrong? Is this behavior by design?

<form name="nameForm" action="#" method="post"
onsubmit="this.name.value = this.name.value + ' (' + location.hostname + ')'">
<input type="text" name="name"
placeholder="Your name" required="required" value="" />
<input type="submit" name="commit" value="Submit" />
<p>This form has a required field.</p>
But <a href="javascript:document.nameForm.submit()">this link</a>
which calls the form's "submit()" function doesn't fire the "required" handler.

Ignore the console error on submit. I have a real submit action.


A workaround is to call document.nameForm.commit.click() from the link, but I still don't understand why the submit action is different.