Skip to main content

Checks if the given element is focused.

/**
 * Checks if the given element is focused.
 *
 * Use Document.activeElement to determine if the given element is focused.
 *
 * https://www.30secondsofcode.org/js/s/element-is-focused
 *
 * @param {HTMLElement} el
 * @return {Boolean}
 */
const elementIsFocused = (el) => el === document.activeElement;

// Examples

elementIsFocused(el); // true if the element is focused

// ---------------------------------------------------------
// ES5 version
// ---------------------------------------------------------
var elementIsFocused = function (el) {
  return el === document.activeElement;
};