Skip to content Skip to sidebar Skip to footer

Retrieving Previously Focused Element

I would like to find out, in Javascript, which previous element had focus as opposed to the current focus. I've been looking through the DOM and haven't found what I need, yet. Is

Solution 1:

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 elementvar prevFocus;

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

          // let's check if the previous focus has already been definedif (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

Solution 2:

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 objectwindow.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 laterwindow.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.

Solution 3:

Well depending on what else your page is doing, it could be tricky, but for starters you could have a "blur" event handler attached to the <body> element that just stashes the event target.

Solution 4:

To me this seems a slight improvement on Gone Coding's answer:

window.currFocus = document;
// Catch focusin 
$(window).on( 'focusin', function () {
  window.prevFocus = window.currFocus;
  console.log( '£ prevFocus set to:');
  console.log( window.currFocus );
  window.currFocus = document.activeElement;
});

... there's no stipulation in the question that we're talking exclusively about INPUTs here: it says "previous elements". The above code would also include recording focus of things like BUTTONs, or anything capable of getting focus.

Solution 5:

Here is a slightly different approach which watches both focusin and focusout, in this case to prevent focus to a class of inputs:

<input type="text" name="xyz" value="abc"readonlyclass="nofocus">
<script>
$(function() {
        var leaving = $(document);
        $(document).on('focusout', function(e) {
                leaving = e.target;
        });
        $( '.nofocus' ).on('focusin', function(e) {
                leaving.focus();
        });
        $( '.nofocus' ).attr('tabIndex', -1);
});
</script>

Setting tabIndex prevents keyboard users from "getting stuck".

Post a Comment for "Retrieving Previously Focused Element"