Retrieving previously focused element

Each time an element is focused, you'd have to store which one it was. Then when another element is focused, you could retrieve the variable for the previous focused element.

So basically, your single focus handler would do 2 things:

  1. Check if previousFocus is defined. If it is, retrieve it.
  2. Set previousFocus to the currently focused element.

Here is a quick demo with jQuery (you can use raw JS too... just fewer lines w jQuery, so it's easier to understand imo):

  // create an anonymous function that we call immediately
  // this will hold our previous focus variable, so we don't
  // clutter the global scope
(function() {

      // the variable to hold the previously focused element
    var prevFocus;

      // our single focus event handler
    $("input").focus(function() {

          // let's check if the previous focus has already been defined
        if (typeof prevFocus  !== "undefined") {

              // we do something with the previously focused element
            $("#prev").html(prevFocus.val());
        }

          // AFTER we check upon the previously focused element
          //   we (re)define the previously focused element
          //   for use in the next focus event
        prevFocus = $(this);
    });
})();

working jsFiddle


Just found this question while solving the exact same problem and realised it was so old the jQuery world has moved on a bit :)

This should provide a more effective version of Peter Ajtais code, as it will use only a single delegated event handler (not one per input element).

// prime with empty jQuery object
window.prevFocus = $();

// Catch any bubbling focusin events (focus does not bubble)
$(document).on('focusin', ':input', function () {

    // Test: Show the previous value/text so we know it works!
    $("#prev").html(prevFocus.val() || prevFocus.text());

    // Save the previously clicked value for later
    window.prevFocus = $(this);
});

JSFiddle: http://jsfiddle.net/TrueBlueAussie/EzPfK/80/

Notes:

  • Uses $() to create an empty jQuery object (allows it to be used immediately).
  • As this one uses the jQuery :input selector it works with select & button elements as well as inputs.
  • It does not need a DOM ready handler as document is always present.
  • As the previously focused control is required "elsehere" is is simply stored on window for global use, so it does not need an IIFE function wrapper.

Tags:

Javascript