Published at
Updated at
Reading time
This post is part of my Today I learned series in which I share all my web development learnings.

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 onsubmit="alert(2)">
  <!-- this won't -->
  <input name="foo" />
  <input name="bar" />

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!

Was this TIL post helpful?
Yes? Cool! You might want to check out Web Weekly for more quick learnings. The last edition went out 11 days ago.
Stefan standing in the park in front of a green background

About Stefan Judis

Frontend nerd with over ten years of experience, freelance dev, "Today I Learned" blogger, conference speaker, and Open Source maintainer.

Related Topics

Related Articles