Forum Moderators: open
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.
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.
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.