Each time an element is focused, you will need to save it. Then, when another element is focused, you can get a variable for the previous focused element.
So, your single focus handler will do 2 things :
- Check if the value of previousFocus is defined. If so, remove it.
- Set the previous focus to the current focused item.
jQuery ( raw JS ... w jQuery, imo):
(function() {
var prevFocus;
$("input").focus(function() {
if (typeof prevFocus !== "undefined") {
$("#prev").html(prevFocus.val());
}
prevFocus = $(this);
});
})();