6

I need to retrieve the DOM element that causes a focusout (blur) event from WITHIN the blur event. The following code will give me the ID of the element that lost focus, NOT the element that caused the element to lose focus. It is that second element that I need.

.live('blur', function(e) {
    var id = $(this).attr('id');
}

How do I get the element that caused the blur, not the element the blur is attached to? The only way I can think of is to capture the window.click event and then handle the logic I need there, but that will get tricky so I am hoping there is a way to get the DOM element from within the blur event.

AlexGad
  • 6,612
  • 5
  • 33
  • 45
  • 2
    http://stackoverflow.com/questions/121499/when-onblur-occurs-how-can-i-find-out-which-element-focus-went-to – Thomas Shields May 05 '11 at 23:01
  • Can you give us more information on what you are trying to achieve once you have the element that caused the blur? There may be other ways to solve the problem – Dean North May 05 '11 at 23:09
  • I need to insure certain actions occur based upon what was or was not clicked - ie: certain elements need to be displayed or hidden. The problem is that this code is legacy code I am loathe to mess with. I can solve this by handling the window click and placing all my logic there, but that would require completely gutting the rest of the code base which I desperately want to avoid. Unless there is some way for me to reliably get the trigger element from within the blur event itself, then I'll have no choice. – AlexGad May 05 '11 at 23:30
  • Thomas' link provided me a solution using a timeout. Not a preferred solution, but it will take care of the issue for now until I can rewire things in the Window.click event. – AlexGad May 06 '11 at 00:35

3 Answers3

18

With this:

$(document).click(function(event) {
   window.lastElementClicked = event.target;
});

Cheers

Edgar Villegas Alvarado
  • 18,204
  • 2
  • 42
  • 61
  • 1
    Sorry, I meant to add that I need the element to be retrieved reliably from within the blur handler. If I setup the above, and then try to use the code: alert($(window.lastElementClicked).attr("id")); from within the blur event, I get the ID of the element the blur is attached to. – AlexGad May 05 '11 at 23:25
3

Run this

$(document).click(function(e) {
    e = e || event;
    $.lastClicked = e.target || e.srcElement;
});

then you can get it anywhere by

var lastClickedElement = $.lastClicked;
// ...
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 1
    Problem though is that there is no guarantee on when the events get triggered although I could be wrong. Wouldn't there be a slight chance that if I tried to get the value that was set in the document click from within the blur event, that it might not have been populated yet? – AlexGad May 05 '11 at 23:11
1
$(document).click(function(event) {
    var selected_name  =  event.target.name;
    var selected_id    =  event.target.id;
    var selected_value =  event.target.value;
    // etc.
});

Just building and clarifying on the posts of others.