Crafting the perfect online form is as much an art as is it engineering. There are so many subtle features we must consider. The ultimate goal is to make our user's task as easy and stress free as possible.
No one likes to fill out a long form. Seeing a long form creates instant anxiety. A common practice is to break long forms into small, digestible bites. Often this uses panels with either tabs or even a wizard style user experience.
This improves user success but creates new challenges for developers.

Recently I was asked by a team how to set the focus to the initial input field as panels are toggled. This requires a level 5 magic spell!!!
Well not level 5, but it does need some simple JavaScript and an intermediate knowledge of CSS selectors.
From here I will share an example of a large form (well let's assume it is large), broken into multiple panels. I will then show how I automagically trigger the initial input field focus as panels toggle. In the end many of you will level up your front-end web development skills a notch or two. You can also apply this knowledge to other areas of your applications and streamline even more code!
All the source code is available on GitHub, in my giant demos and samples project. I have to warn you this is a scratch pad of me working through ideas so it is a bit chaotic. The demo page can be found within the JavaScript folder.
There are 2 files you can add to your websites and progressive web apps, tab-panel.js and tab-panel.css. They are located in the repo's /js and /css folders. You will see how I use the JavaScript module and CSS classes to create the user experience. You can extend them to fit your needs.
In this article, I won't focus on the construction of the tab and tab panels. We will explore toggling between panels and setting focus to the desired input field. I have another article I am working on to detail the tab panel setup. You will see how the JavaScript library handle's selecting a tab and associated workflow.
Creating Tab Panels with HTML and CSS
The first step is to create a set of tabs and the corresponding panels in HTML. Because this is a form, I like to wrap the FORM tags around the tab panel. The first row is the set of tabs or pills as they are sometimes called. These tabs serve as the navigation elements. For this example, I have three tabs and corresponding panels.
The next row wraps the panels. Inside of this row is the set of tab panels.
If you examine the tab elements you will see a custom attribute, tab-panel-id. The attribute is used as the visible panel is changed. It will set the automatically focused input field. You will see how that is done shortly.
<form>
<div class="row col-12 my-2 px-0 tab-container">
<div class="tab-pill-slider-container">
<ul class="tab-pills">
<li class="tab-pill mx-0 text-center">
<div class="tab-tab active" tab-panel-id="panel-1">
Tab 1
</div>
</li>
<li class="tab-pill mx-0 text-center">
<div class="tab-tab" tab-panel-id="panel-2">Tab 2</div>
</li>
<li class="tab-pill mx-0 text-center">
<div class="tab-tab" tab-panel-id="panel-3">Tab 3</div>
</li>
</ul>
</div>
<div class="tab-content clearfix col-12">
<div class="tab-pane panel-1 flex-column active" id="panel-1">
<!-- Form fields here -->
</div>
<div class="tab-pane panel-2 flex-column" id="panel-2">
<!-- Form fields here -->
</div>
<div class="tab-pane panel-3 flex-column" id="panel-3">
<!-- Form fields here -->
</div>
</div>
</form>
Inside each one of the panels are the actual form input fields. Normally, in a well designed form, the first input field will have the autofocus attribute set.
When a page is rendered the browser automatically looks for the first autofocus attribute. It then sets the focus there. If there are more than one designated the extras are ignored. If there are no input fields designated as autofocus, then nothing is set to an initial focus.
In this example the first input, exampleInputEmail1, has the autofocus attribute applied.
<div class="tab-pane panel-1 flex-column active" id="panel-1">
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" id="exampleInputEmail1" autofocus
aria-describedby="emailHelp" placeholder="Enter email" tabindex="100">
</div>
<div class="form-group">
<label for="exampleInput1">Next Field</label>
<input type="text" class="form-control" id="exampleInput1" placeholder="Some Value" tabindex="110">
</div>
<div class="form-check">
<input type="checkbox" class="form-check-input" id="exampleCheck1" tabindex="120">
<label class="form-check-label" for="exampleCheck1">Check me out</label>
</div>
</div>



Join the conversation! Your thoughts help the community grow.