1

TIL - submit() versus requestSubmit()

 1 month ago
source link: https://www.raymondcamden.com/2024/03/01/til-submit-versus-requestsubmit
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.


Raymond Camden

Father, husband, developer relations and web standards expert, and cat demo builder.

March 1, 2024

TIL - submit() versus requestSubmit()

TIL - submit() versus requestSubmit()

Today I learned (well, technically, a few days ago, the week has been a lot), that the web platform supports a requestSubmit method. Since the beginning of time (or the beginning of JavaScript), we've been able to submit forms like so:

var myForm = document.getElementById('theform');
myForm.submit();

I intentionally used getElementById there as a reminder of what we had before jQuery. Given that, why do we need another method? Two important requests.

Reason the First #

When using submit, any validation of form fields is completely skipped. Consider this form:

<form method="post" id="theForm">
	
	<p>
	<label for="name">Name</label>
	<input id="name" name="name" required>
	</p>

	<p>
	<label for="email">Email</label>
	<input id="email" name="email" type="email" required>
	</p>

	<p>
	<input type="submit">
	</p>
</form>

I've got two fields, both required, with the second field using type email. If you hit submit, the form will stop itself from POSTing and show errors, but if you submit with JavaScript, that validation is completely ignored.

I added two more buttons to my HTML:

<p>
	<button id="testSubmit">Test submit()</button>
	<button id="testRequestSubmit">Test requestSubmit()</button>
</p>

And wrote some quick JavaScript to demo this:

const $form = document.querySelector('#theForm');

document.querySelector('#testSubmit').addEventListener('click', () => { $form.submit() });

document.querySelector('#testRequestSubmit').addEventListener('click', () => { $form.requestSubmit() });

Clicking the first button immediately shows that validation is ignored. Clicking either the main submit button in the form or the tester button shows validation working.

Screenshot showing validation being run on the first formfield

Reason the Second #

Not only is validation ignored with submit(), any submit handler on the form itself is completely ignored. I added this:

$form.addEventListener('submit', e => {
	console.log('submit fired on form');
	e.preventDefault();
});

And again, submit() ignores it and requestSubmit() runs it fine. I'm mostly sure I remember this aspect of submit(), but it's definitely been a while since I've thought about it.

Anyway, everyone loves the web platform. (Except Apple.) Here's a CodePen showing this in action if you want to see for yourself. (Which is 100% why most of my blog posts exist.)

Support this Content!

If you like this content, please consider supporting me. You can become a Patron, visit my Amazon wishlist, or buy me a coffee! Any support helps!

Want to get a copy of every new post? Use the form below to sign up for my newsletter.

Share: Threads Twitter Facebook LinkedIn

Webmentions

3 Likes and 1 Retweet


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK