• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
JavaScriptSource

JavaScriptSource

Search 5,000+ Free JavaScript Snippets

  • Home
  • Forum
  • Browse Snippets
    • Addon
    • Ajax
    • Buttons
    • Cookies
    • CSS
    • Featured
    • Forms
    • Games
    • Generators
    • Image Effects
    • Math Related
    • Miscellaneous
    • Multimedia
    • Navigation
    • Page Details
    • Passwords
    • Text Effects
    • Time & Date
    • User Details
Home / Miscellaneous / Detect If an Element has focus

Detect If an Element has focus

Detect If an Element has focus

To detect if an element has the focus, you use the read-only property activeElement of the document object:

const el = document.activeElement

o detect if an element has focus, you compare it with the document.activeElement.

The following checks if the input text with the .username class has the focus:

const el = document.querySelector('.username');
console.log(el === document.activeElement);

Miscellaneous

Related Snippets:

  • Sanitize an HTML string to reduce the risk of XSS attacks
  • Create a new array from an existing one
  • Check if the provided value is of the specified type
  • Transform all text in a string to lowercase

Primary Sidebar

FREE UPDATES!

Get the latest updates in your inbox for FREE!

Popular Posts

Story Generator

IP Grabber

Simple Calendar

Remove Ads

Astrological Calculator

Copyright © 2022 JavaScriptSource.com

  • About
  • Privacy Policy
  • Submit
  • FAQ
  • Jobs For Developers
  • Contact Us