Javascript field with focus

Is there a way to determine which filed has the focus?
Who is Participating?
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(" /><br />
<input type="text" name="field2" onfocus="savefoc(" /><br />
<input type="text" name="field3" onfocus="savefoc(" /><br />
<input type="hidden" name="savefocus" value="" />

Yury MerezhkovDevelopment Team LeadCommented:
Try this:

<script language="javascript">
var hasFocus

function grabFocus(e) {
var obj
if (e) {
     obj =
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:

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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.