Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking if a text field has focus

Status
Not open for further replies.

stewartwebb

Programmer
Jan 6, 2004
92
GB
Hi all,

Is it possible to know whether a field has focus?

Basically what i'm doing at the moment, I have an 'onDblClick' on the form tag which calls a function.
When I double click on any text field on the form the function is called (this works). Now what i'm doing is looping through all the elements on the page and if the element is 'text', I want to check if this field has the focus after the user has double clicked it. Is this possible?

Thanks

Stewart.
 
to me, this would make much more sense if you called the ondblclick event from the text field itself, not the form. you could then pass in the object itself to the function, and you'd already know which field had focus.

*cLFlaVA
----------------------------
[tt]I already made like infinity of those at scout camp...[/tt]
beware of active imagination: [URL unfurl="true"]http://www.coryarthus.com/[/url]

BillyRayPreachersSonIsTheLeetestHax0rDude
[banghead]
 
To do this cross-browser, I believe you'll have to attach onfocus event handlers to all your inputs, updating a global variable with the last element to receive focus.

Hope this helps,
Dan


[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
At the moment the onDblClick is set on the input fields but i'm trying to do a global event so this does not need to be done on every field.
This will only ever be used in IE.
Any more suggestions?

Thanks

Stewart
 
what BRPS said makes sense to me. I don't really like the concept as a whole - seems kinda hack-y.

*cLFlaVA
----------------------------
[tt]I already made like infinity of those at scout camp...[/tt]
beware of active imagination: [URL unfurl="true"]http://www.coryarthus.com/[/url]

BillyRayPreachersSonIsTheLeetestHax0rDude
[banghead]
 
rather than code a million times:
Code:
<input [COLOR=red]ondblclick="foo"[/color] />
<input [COLOR=red]ondblclick="foo"[/color] />
<input [COLOR=red]ondblclick="foo"[/color] />

use a body.onload event to attach event handlers to your inputs:
Code:
<script type="text/javascript">
function attachEventHandlers() {
  inputs = document.getElementsByTagName('input');
  for (i=0;i<inputs.length;i++) {
    inputs[i].ondblclick=foo;
  }
}

function foo(e) {
  e = (e)?e:event;
  inputObject = (e.srcElement)?e.srcElement:e.target;
  

  [COLOR=green]// inputObject is your <input> object that was dblclicked.
  // custom code as required here[/color]
}
</script>

<body onload="attachEventHandlers()">

<marc>
New to Tek-Tips? Get better answers - faq581-3339
 
Hang on a minute - something doesn't quite makes sense here. You said:

I want to check if this field has the focus after the user has double clicked it

Any input field, after being double-clicked, will have focus. So why not just use "this" in your double-click handler to know which field was double-clicked, and thus have focus?

Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top