Solved

Javascript field with focus

Posted on 2006-11-03
4
647 Views
Last Modified: 2012-08-13
Is there a way to determine which filed has the focus?
0
Comment
Question by:prowebinteractive
4 Comments
 
LVL 7

Expert Comment

by:RealSnaD
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
</script>
0
 
LVL 13

Expert Comment

by:AngryBinary
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:

http://www.anaesthetist.com/mnm/javascript/part4.htm#event
0
 
LVL 5

Accepted Solution

by:
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(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="" />

Regards,
Mukesh
0
 
LVL 2

Expert Comment

by:bappaditya143
ID: 17872477
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
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Where can I find... 1 53
Designing forms 3 45
Any Way to rotate banner ads from Commission Junction? 2 37
How to post data to an API using ASP Classic 3 31
Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
The viewer will learn how to dynamically set the form action using jQuery.
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

726 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