Forum Moderators: open

Message Too Old, No Replies

How do I find the field that has focus()

         

habman

8:19 pm on Jul 19, 2004 (gmt 0)



Hello all. Newbie here.

The following javascript will place focus on the "i" element in a form.

---- document.forms[0].elements[i].focus(); ----

Now here is my dilemma.

If I want to find out which field has focus in my form how would I go about doing that?

Thanks in advance for any assistance.

coopster

10:23 pm on Jul 19, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, habman!

MS IE has a property,

activeElement
, that can be examined (
document.activeElement
) to see which element currently has focus. However, that is IE-specific and won't work in all browsers.

One option may be to create a new variable and set it's value to the element index number in the form whenever that element receives focus.

Bernard Marx

10:41 pm on Jul 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I messed around with this for a while, then gave up trying to get a simple X-browser solution. document.activeElement does seem to be the way to go for IE.

The XB approach?
Using a global to hold a reference to the current focused element would, I reckoned, be easy enough. The 1st problem is that the var will continue to hold an element if focus is lost by the document being clicked on. The solution would be to use onblur handlers to set the variable back to null, but this doesn't work - it seems that, when switching between one element and another, the blur event is fired after the focus, so messing everything up.

The best a can think of is to use a global, as coopster suggests, which is set by onfocus. Clicks on the document will set the global to null, but click events on the actual form elements will have to be prevented from bubbling, or filtered out.

Rambo Tribble

5:29 am on Jul 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



While I think coopster's approach probably has more merit, if you only have one form, and it isn't too long, it should be reasonably efficient to simply loop through the elements array and determine if one has focus. This assumes you are only looking for focus on a form element, which is what I took you to be saying. You could initiate the looping function from an onclick event on the body element. Bear in mind, the function might return nothing if no form element has focus (in other words, some other element within the body has focus other than a form element).

Bernard Marx

8:08 am on Jul 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



loop through the elements array and determine if one has focus

Rambo, how do you do that? I couldn't find a property to query.

Rambo Tribble

1:15 pm on Jul 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Good point, Bernard. Although Moz offers the :focus pseudo-class and focusNode, one of which might be forced into service, IE presents no such possibility.

Bernard Marx

2:53 pm on Jul 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I got something wrong again. D'oh factor 10:
While testing using onfocus/onblur to update a global variable, I was using alerts to test the variable, upsetting the whole thing with more blurs.

It seems like a viable method, though as you said, inconvenient.