We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now


Javascript field with focus

Medium Priority
Last Modified: 2012-08-13
Is there a way to determine which filed has the focus?
Watch Question

Yury MerezhkovDevelopment Team Lead

Try this:

<script language="javascript">
var hasFocus

function grabFocus(e) {
var obj
if (e) {
     obj = e.target
else {
     obj = event.srcElement
var tag = obj.tagName+""
tag = tag.toLowerCase()
if (tag=="input") hasFocus = obj
  else hasFocus = null

//Now you can check anywhere on the page does some input element has focus or not

document.onclick = grabFocus
In order to keep track of which element has focus (regardless of whether or not the user is using their mouse or setting focus with the 'tab' key), create an onFocus and onBlur event for the elements you'd like to monitor. When an event is raised, simply track the sender in another variable.

Despite the annoying javascript foolery on the page, this site describes how to use events:

You can call a simple javascript on "onfocus" property of each control. And pass name of control to that fucntion. Now you can save the value of control having focus in any hiddenvalue control as shown below:

Javascript Function:

function savefoc(fldname)
document.form[0].savefocus.value = fldname;

Apply this function to "onfocus" property of each control and take a hidden control to store the value of control having focus:

<input type="text" name="field1" onfocus="savefoc(this.name)" /><br />
<input type="text" name="field2" onfocus="savefoc(this.name)" /><br />
<input type="text" name="field3" onfocus="savefoc(this.name)" /><br />
<input type="hidden" name="savefocus" value="" />


Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

this a code pls put step by step



 function putFocus(formInst, elementInst) {
  if (document.forms.length > 0) {


<BODY onLoad="putFocus(0,1);">

<div align="center">
<form method="post" name="bogus" id="bogus-form">
<input type="text" name="bogus_field0" id="bogus_field0" size="20" maxlength="12"><br>
<input type="text" name="bogus_field1" id="bogus_field0" size="20" maxlength="12" value="Form focus is here!"><br>
<input type="text" name="bogus_field2" id="bogus_field0" size="20" maxlength="12">

Bappaditya Majumder
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.