Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Javascript field with focus

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

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

by:Yury Merezhkov
ID: 17871050
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
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 2000 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(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="" />


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

Understanding Web Applications

Without even knowing it, most of us are using web applications on a daily basis. Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We often confuse these web applications tools for websites.  So, what is the difference?

Question has a verified solution.

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

Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

596 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