Search code examples
javascriptjqueryeventslost-focus

How do i find the new focus item with jquery?


I have a pop up dialog that lets you write text and does stuff when you click a button. My code is below

This function works, i find the new object by looking at e.originalEvent.explicitOriginalTarget. However now i notice if i press tab this function will be called but e.originalEvent.explicitOriginalTarget will give me the same current object instead of the new object. So my dialog doesnt close if a user presses tab to leave. How do i find the correct new dom item?

$('#Area').focusout(function (e) {
    if ($(e.originalEvent.explicitOriginalTarget).closest('#Area').size() == 0)
        $('#Area').hide();
});

Solution

  • Check out this question/answer How to select an element that has focus on it with jQuery

    I think the reason why you don't get anything with $("*:focus"); in Firebug console is when you click the console, the element loses focus.

    And if you want to tackle it with events, the opposite of focus() is blur().


    Edit

    Maybe you can even try a different approach. If your only concern is watching for tab key, you can use .keypress() event and watch for tab keycode which is 9.