views:

342

answers:

3

I like to know what the onFocus event is (if there will be any) when I receive an onBlur event in my (javascript) handler. How is this possible?

Example: I have a group of textboxes that form one group. Only if the whole group loses focus I want to inform subscribers about the group onBlur event. So if an onBlur event occurs I only want to forward it when I notice that it's not followed by an onFocus event on another textbox in the group..

I do this now through a delay and looking if a focus event occurred in the meantime. But I don't like this solution so much as: because of the delay the blur occurs after the focus and the event that it contained in the delayed blur is "old" which can be tricky in further processing..

My idea was: looking in the "event queue", when I receive the blur event, to detect any onFocus event that will follow... But I have no idea if this is possible and how to do this :(...

A: 

-- Edit:

Having reviewed your question I'm not quite sure what you're asking, so I'm not sure if what I've posted is suitable. Regardless, I leave it here for consideration.

You can do it quite trivially with a map and the 'this' variable:

var map = new Object();

...

function doBlur (obj) {
   if(map[obj] == "focus"){
   }
}

function doFocus (obj) {
    map[obj] = "focus";
}

<input onblur="doBlur(this);" onfocus="doFocus(this);" />

But even further, given that only one textbox can be focused at a time, you can just have a variable named lastFocus, and check that that object equals the one you want.

Noon Silk
Tanks, but I think we have a misunderstanding.What I want: How can I determine if a focus event will follow the blur event, when I am in your doBlur method.Hope this makes it more clear.
edbras
Well, you can't. You can only check that be setting a timeout, and waiting to see if a focus() event is fired before that timeout is hit. (or just seeing if the focus() event is ever fired again, when the `obj` instances are different).
Noon Silk
A: 

You can use delegation method for blur and focus
So you will check what was focused.

Eldar Djafarov
Thanks, but could you give more details?Note: If I receive my blur event I want to know if a Focus event is coming as one of the next event... I can't see how this can be done with delegation/event capturing..
edbras
Using Event delegation you can process all your events in one place. You can create a global(or singleton if you prefer clearer solution) to store last focus-event-target or blur-event-target. And change behavior of your script in this point depending on previous event-target.
Eldar Djafarov
So I can't look if a focus event is coming after the blur event when processing the blur event?
edbras
No, you can't .
Eldar Djafarov
+1  A: 

The focus and the blur event should be added to the event queue, so a simple 1-100 ms setTimeout would probably do the trick.

I know you aren't using jQuery - but its easier for me to air code an example using it:

var blurTimeout;

function blurAlert() {
   alert('Lost Focus');
}

// assign these two simple functions to all inputs on the page.
$('input').blur(function() {
  blurTimeout=setTimeout(blurAlert, 1);
}).focus(function() {
  clearTimeout(blurTimeout);
});

I just tested it out at 1ms in firefox, I'm pretty sure that timing will hold well for other browsers as well. Because of the way events are handled, blur fires, then focus, then the timer.

If you really want to make sure to keep the this around from the event your event handler turns into:

 function() { 
   blurTimeout = setTimeout(function() {
     blurAlert.apply(this,arguments);
   }, 1);
 }
gnarf
I don't understand you solution.Why do you propose this?, As I my question I explain that the timer-solution I have now......What I want: know if a focus event will follow after the blur event...
edbras
The only way to determine if a focus event will follow your blur event is to delay processing the blur event until after the focus event that is already in the queue. A 1ms timeout delays the script execution of your blur handler until after you can process the focus event.
gnarf