• Web APIs
  • ariaAtomic
  • ariaBusy
  • ariaColIndexText
  • ariaDisabled
  • ariaKeyShortcuts
  • ariaModal
  • ariaPlaceholder
  • ariaRelevant Non-standard
  • ariaRowIndex
  • ariaSetSize
  • ariaValueNow
  • childElementCount
  • clientHeight
  • currentCSSZoom
  • elementTiming Experimental
  • lastElementChild
  • outerHTML
  • scrollHeight
  • scrollLeftMax Non-standard
  • scrollTopMax Non-standard
  • tagName
  • Instance methods
    1. attachShadow()
    2. computedStyleMap()
    3. getAttributeNode()
    4. getClientRects()
    5. getHTML()
    6. hasPointerCapture()
    7. matches()
    8. releasePointerCapture()
    9. removeAttributeNS()
    10. requestPointerLock()
    11. scrollIntoViewIfNeeded() Non-standard
    12. setAttributeNodeNS()
    13. setCapture() Non-standard Deprecated
    14. afterscriptexecute Non-standard Deprecated
    15. animationstart
    16. beforematch Experimental
    17. beforescriptexecute Non-standard Deprecated
    18. beforexrselect Experimental
    19. compositionstart
    20. copy
    21. DOMActivate Deprecated
    22. DOMMouseScroll Non-standard Deprecated
    23. fullscreenchange
    24. gesturechange Non-standard
    25. gestureend Non-standard
    26. gesturestart Non-standard
    27. keypress Deprecated
    28. mouseenter
    29. mouseover
    30. mousewheel Non-standard Deprecated
    31. MozMousePixelScroll Non-standard Deprecated
    32. pointerenter
    33. pointerover
    34. pointerrawupdate Experimental
    35. scrollsnapchange Experimental
    36. scrollsnapchanging Experimental
    37. touchmove
    38. transitionrun
    39. webkitmouseforcechanged Non-standard
    40. webkitmouseforcedown Non-standard
    41. webkitmouseforceup Non-standard
    42. webkitmouseforcewillbegin Non-standard
    43. Learn more
    44. See full compatibility
  • The focus event fires when an element has received focus. The event does not bubble, but the related focusin event that follows does bubble.

    The opposite of focus is the blur event, which fires when the element has lost focus.

    The focus event is not cancelable.

    Syntax

    Use the event name in methods like addEventListener(), or set an event handler property.

    js
    addEventListener("focus", (event) => {});
    
    onfocus = (event) => {};
    

    Event type

    Event properties

    This interface also inherits properties from its parent Event.

    FocusEvent.relatedTarget

    The element losing focus, if any.

    Examples

    Simple example

    HTML

    html
    <form id="form">
      <label>
        Some text:
        <input type="text" placeholder="text input" />
      </label>
      <label>
        Password:
        <input type="password" placeholder="password" />
      </label>
    </form>
    

    JavaScript

    js
    const password = document.querySelector('input[type="password"]');
    
    password.addEventListener("focus", (event) => {
      event.target.style.background = "pink";
    });
    
    password.addEventListener("blur", (event) => {
      event.target.style.background = "";
    });
    

    Result

    Specifications

    Specification
    HTML
    # handler-onfocus

    Browser compatibility

    BCD tables only load in the browser

    See also