Implicit form submission doesn't work always
- Published at
- Updated at
- Reading time
My friend Tom and Robin recently had a discussion in our of our Slack channels about implicit form submissions. An implicit form submission is the action of just pressing the ENTER key when focussing an input element. It turns out that this is only possible under two conditions:
- the form has a submit button
- the form has only one input element
So let me quickly share Tom's example.
<form onsubmit="alert(1)"> <!-- this will alert if you press enter while "foo" is focused --> <input name="foo" /> </form> <form onsubmit="alert(2)"> <!-- this won't --> <input name="foo" /> <input name="bar" /> </form>
You could work around this behavior by adding a hidden submit button. But still... this behavior surprised me.
I didn't know about it and it's defined in the spec. Good to know!
- Emmet VS Code bindings to level up HTML editing
- How to tell browsers that your site supports color-schemes
- How to preload responsive images with imagesizes and imagesrcset
- The navigation timing API includes the type of the current navigation
- Define where an element should be scrolled to using elem.scrollIntoView