PDA

View Full Version : Get focused element in a form



armandoxxx
23 May 2012, 12:12 AM
Hey

Is there a way to get focused element in a form ?

Regards

Armnado

sword-it
24 May 2012, 10:47 PM
Hi!

Use document.activeElement, it is supported in all major browsers.

For more details on document.activeElement browser compatibility, check this https://developer.mozilla.org/en/DOM/document.activeElement#Browser_compatibility


If You are not still getting document.activeElement in your form, try to do following:-




if (!document.activeElement) {
//Step 1:- Add a "focus" event handler to all fields .
//Step 2:- Record the last-focused field in a variable.
//Step 3:- Add a "blur" handler to clear the variable upon a blur event for the last-focused field.
}