Link to home
Start Free TrialLog in
Avatar of prowebinteractive
prowebinteractive

asked on

Javascript field with focus

Is there a way to determine which filed has the focus?
Avatar of Yury Merezhkov
Yury Merezhkov
Flag of United States of America image

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
</script>
Avatar of AngryBinary
AngryBinary

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:

http://www.anaesthetist.com/mnm/javascript/part4.htm#event
ASKER CERTIFIED SOLUTION
Avatar of prajapati84
prajapati84
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi,

this a code pls put step by step


<HEAD>

<SCRIPT LANGUAGE="JavaScript">

 function putFocus(formInst, elementInst) {
  if (document.forms.length > 0) {
   document.forms[formInst].elements[elementInst].focus();
  }
 }
</script>

</HEAD>


<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">
</div>
</body>

Regards,
Bappaditya Majumder