Software, your way.
burger menu icon
WillMaster

WillMaster > LibraryManaging Website Forms

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Focus In Specific Form Field On Page Load

When a web page with a form loads in a browser, a specific form field can obtain keyboard focus. Ready to type. So you don't have to click into it first.

This article is for implementing the functionality equally for all popular browsers, even IE, regardless which HTML version is used. It does require JavaScript.

(As of this writing, IE does not support HTML5. HTML5 has a new form attribute named "autofocus" to automatically give a specific form field focus when the page loads.)

Example implementation

This example puts keyboard focus on the second form field:



The above form field will have obtained focus when the web page loaded.

Implementing auto form field focus on page load

To implement, the form field to automatically obtain focus needs to have an id. Some JavaScript is run when the page is loaded, which gives the form field its focus.

Here is an example form field with an id.

<input type="text" id="field-id" name="field_name">

And here is the JavaScript.

<script type="text/javascript">
// Specify the id value of the form field to automatically 
//    obtain focus when the page loads.

var FieldID = "field-id"

// End of customization
document.getElementById(FieldID).focus();
</script>

Where to put the JavaScript

Put the JavaScript somewhere below the form so the JavaScript runs after the form source code is loaded in the browser. It can be immediately below the form or at the end of the page next to the cancel </body> tag.

Example implementation source code

Here is the complete source code for the above example implementation.

<form onsubmit="return false;">
<input type="text" id="field-id1" name="field_name1" style="width:200px;"><br>
<input type="text" id="field-id2" name="field_name2" style="width:200px;"><br>
<input type="text" id="field-id3" name="field_name3" style="width:200px;">
</form>

<script type="text/javascript">
// Specify the id value of the form field to automatically 
//    obtain focus when the page loads.

var FieldID = "field-id2"

// End of customization
document.getElementById(FieldID).focus();
</script>

That's the simplicity of it.

With HTML5, it's even simpler. Just put autofocus="autofocus" into the form field to get the focus, instead of the above implementation instructions. (See HTML5 input autofocus Attribute for HTML5 "autofocus" info.)

If it is important that your web page has the form field focus functionality in all browsers, implement it according to this article.

Bookmark this article or put it on your twitter feed or favorite it, whatever you do to keep track of information you may need to refer to later on.

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC