document.activeElement in Firefox - Finding The Element That Has Focus

Internet Explorer has a handy 'activeElement' property on the document object which keeps track of the current element that has focus. Firefox 3 is going to support the same property inline with the HTML 5 specification but that's a fair way off so here's a work around:

function onElementFocused(e)
{
    if (e && e.target)
        document.activeElement =
        e.target == document ? null : e.target;


if (document.addEventListener)
    document.addEventListener("focus", onElementFocused, true);

All we do here is assign a handler to the focus event of the document. Our handler updates the activeElement property. 

Just place that in your global javascript code and you've got activeElement support in mozilla!

Jimmy 

Related posts

Comments

March 31. 2008 09:42

Thanks! Just what the doctor ordered. I am working with some legacy code that depends on activeElement, and this workaround is much better than others I've seen that use a focus/blur handler on event element to track the active element.

Good stuff!
KR

KR

March 25. 2009 00:16

Pingback from irama.org

Blog Archive : Who started it? :
irama.org

irama.org

April 11. 2010 02:48

Pingback from topsy.com

Twitter Trackbacks for

document.activeElement in Firefox - Finding The Element That Has Focus
[jamesgoodfellow.com]
on Topsy.com

topsy.com

Add comment


 

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

September 3. 2010 07:33

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010