If there is an input, there should be a form

This post was originally published on https://tomekdev.com/posts/input-and-form. Go there to read a bit more interactive and style richer version ✌️

I often see in Single Page apps a situation where someone uses just an field. Sometimes in the accompaniment of if you happen to work with a pro 🌟. It feels that when we gained control of inputs with two-way binding and we started handling events on buttons with our fancy frameworks, we forgot the old way of doing things.

The old way, and the right way

The headline of this section may suggest that the old way and the right way are two different things. Where in fact, it’s the opposite. If you come back with your memory before the frameworks (or ajax) era you see that people were using a element when dealing with inputs and buttons.

In the past we were extensively using to process the input. I encourage you to still use a form element. It comes with benefits.

Common mistake

I use the keyboard to navigate and interact with pages. That speeds up everything. So for me one of the biggest pitfalls of using without a is that the input is not auto-submittable, so to say.

When you hit in the field that is inside the form then the form is submitted. That's very convenient for solo-input interfaces like a modal below:

You don’t have to touch your mouse or touchpad and click button 🎉. That would be a waste of time!

Solution

It’s a common mistake to bind your action to a where in fact all you have to do is wrap your with a and assign event to a form:

HTML file:

Javascript:

Don’t forget to prevent default behavior from firing and run your custom behavior. It could be running validation and actually sending the data.

One more thing that you should consider is always specifying the button’s type. It’s by default in most browsers but Internet Explorer is different in this case and has as a default type.

--

--

I connect humans and machines. Usually write about interfaces, digital products, and UX on tomekdev.com. Founder of checknlearn.com. A bit nerdy 🤓

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Tomek Niezurawski

I connect humans and machines. Usually write about interfaces, digital products, and UX on tomekdev.com. Founder of checknlearn.com. A bit nerdy 🤓