Javascript field with focus

Posted on 2006-11-03
Last Modified: 2012-08-13
Is there a way to determine which filed has the focus?
Question by:prowebinteractive

Expert Comment

ID: 17871050
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
LVL 13

Expert Comment

ID: 17872184
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:

Accepted Solution

prajapati84 earned 500 total points
ID: 17872350
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="" />


Expert Comment

ID: 17872477

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

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now