Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine last item clicked

Tags:

jquery

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.

like image 414
AlexGad Avatar asked May 05 '11 22:05

AlexGad


2 Answers

With this:

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

Cheers

like image 139
Edgar Villegas Alvarado Avatar answered Nov 14 '22 22:11

Edgar Villegas Alvarado


Run this

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

then you can get it anywhere by

var lastClickedElement = $.lastClicked;
// ...
like image 4
BalusC Avatar answered Nov 14 '22 23:11

BalusC