SQL PHP HTML JavaScript articles and free code If you are facing any problem in viewing this page, please tell us
 

HTML Set focus on load event by using body tag


In a form we can keep the blinking cursor focused in any component at the time of opening of the page. This way the visitor can directly enter the data or make selection without putting extra effort on selecting the form element. Much time we know the first job for the user is to enter the data in a particular field, so by default we can keep the cursor in that so the data can be entered directly. The user always has the option of changing the cursor or focus on different form element based on the requirement.

For example let us take the case of a login form where the member has to enter the login id and password. As there are two input fields used for this purpose, generally the user will enter user id first and then move to next field to enter password. Here we can keep our focus to the user id field at the time of page open or load so the member can directly enter ID in the text box. So we have to set our focus to the user id field at the time of page load. We can achieve this by using onload command in the body html tag. Like this

<body onLoad="document.f1.userid.focus()">

Our form element can be like this

<form method=post action='' name=f1>
<input type=text name='userid'>
<input type=submit value=Submit></form>


The onLoad command inside the body tag will set the focus to the text field of the form.
Here is the demo
You can always see the cursor in the above text field every time this page reloads.

Read on how window.onload function is used in JavaScript to execute codes as soon as the page loads



Sections
PHP
JavaScript
ASP
HTML
SQL
Photoshop
Articles SEO
List of HTML Tutorial
HTML Tags
HTML Calendar
Tags on web page
Web Design
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.