In normal JavaScript (i.e. without any frameworks like jQuery, MooTools, etc.):
var focusedElement;
document.addEventListener("focus", function(e) {
focusedElement = e.target;
}, true);
document.addEventListener("blur", function(e) {
focusedElement = null;
}, true);
Basically, whenever an element receives focus, we save this element in a variable, and when an element loses focus, we reset the variable.
HTML 5 has a special attribute for accessing the currently concentrated element:
var focusedElement = document.activeElement;
source
share