How to catch focus events on any element in IE?

With Firefox, I can do:
document.addEventListener("focus", gotEvent, false)

Then any focus event on any element on the page will eventually bubble up and my listener 'gotEvent' will be called.

I could register the same listener in IE with:
document.attachEvent("onfocus", gotEvent)

However this listener it never called. My understanding is that it is never called because the focus event does not bubble on IE (see reference to MSDN documentation below). But then, is there a way to catch all the focus events on any elements on my page by registering just one listener in IE?

The profound reason why I want to do this is that I have pretty large pages. So large, that it takes too much time to register my event on all the elements when the page loads. Hence the idea of registering the event handler only once, and figuring out in the event handler code what the target of the event was.

Any suggestion is welcome.

Who is Participating?
Michel PlungjanConnect With a Mentor IT ExpertCommented:
Can't you register them inline - I know that makes very verbose html but if it is so slow?

I had a look.

Perhaps this page and the links on it can help:
Michel PlungjanIT ExpertCommented:
How about only having onFocus on the elements you need onFocus on?
avernetAuthor Commented:
mplungjan: That's the point of my question. There are thousands of elements I would have to register the event handler on. This is what I do now, but registering my event handler on such a large number of elements is quite slow.

Never miss a deadline with

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

avernetAuthor Commented:

I would like to avoid adding the event handlers to the JavaScript code as this will increase the amount of HTML that gets sent to the client, making the page even larger and longer to download. But yes, if nothing else works, this on my list of possible solutions.

Michel PlungjanIT ExpertCommented:
And this link is different from the one he already posted in what manner?
Dear all,
What I tried to said is:
We can use anonymous event handler to catch focus events on all elements in

For example:
var theEvent = typeof document.onactivate == 'function' ? 'onactivate' : 'onfocus';

document[theEvent] = function()
/*Changes applied to all elements*/

Perhaps it has clear out the puzzles.

avernetAuthor Commented:

I tried to register an event handler with document.onfocus = function(event) { ... }, but it does not get called on IE when I set the focus to, say, a text field in the page.

This however works fine with Firefox. From what I understand, this is because the 'focus' event does not bubble on IE, per the reference I was linking to my original message:

Dear Friends,
I have work out an example of how it work in IE 6.
<style type="text/css">
input { background: #ffc; color blue;}
input:hover { background: #ddd; color: maroon;}
input:focus { background: yellow; border: 3px solid red; color: red; }
function initForms(){
       var el;      
        for(i = 0; i < document.forms.length; i++){
                for(j = 0; j < document.forms[i].elements.length; j++){
                        el = document.forms[i].elements[j];
                        el.onfocus = fldBGChange; /*Is a reference, therefore no blacket*/
                        el.onBlur  = fldBGChange; /*Event handler are case sensitive*/


function fldBGChange(){
        bgClr =;
       if(bgClr == '' || bgClr == null){       
       = 'yellow';
        } else {
       = 'white';


<body onLoad="initForms();">
<tr><td><INPUT TYPE="text" NAME="main1"></td></tr>
<tr><td><INPUT TYPE="text"  NAME="main2"></td></tr>
<tr><td><INPUT TYPE="text"  NAME="main3"></td></tr>
Hope it helps.
Michel PlungjanIT ExpertCommented:
@Peter Please take time to read the question more carefully
1. the link you mentioned was the same as he already had
2. "So large, that it takes too much time to register my event on all the elements when the page loads."

means he has tried to do what you suggests but it takes too long!!!
avernetAuthor Commented:
I found a solution using IE DHTML behaviors:

In the HTML file, add:

<style type="text/css">
    * { behavior: url(; }
</style> looks like:

<PUBLIC:ATTACH EVENT="onfocus" ONEVENT="ieFocus()" />


function ieFocus() {
    var target = window.event.srcElement;, window.event);

This way the function ieFocus() is called every time there is a focus event on any element.

And for Firefox, we can to capture the event by using 'true' as the third parameter to addEventListener(). Contrast this with registering a listener which is called when the event bubbles.

window.addEventListener("focus", mozFocus, true);

avernetAuthor Commented:

I asked the points to be assigned to you for your comment earlier (Date: 07/17/2006 11:54PM PDT), but in addition to also to mark my last comment as a alternate answer.

Asked on:

Thank you all,

Michel PlungjanIT ExpertCommented:
Well done!
All Courses

From novice to tech pro — start learning today.