HTML onfocus Attribute

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The onfocus attribute is an event-handler attribute. It runs JavaScript when the focus event fires — a focusable element received focus (mouse click, tap, or keyboard Tab).

Common on <input>, <textarea>, <select>, <button>, and links. Related: onblur (focus left), tabindex, and autofocus. The opposite of focus is blur.

Baseline · Widely available.

Not deprecated and not experimental. Per MDN, focus is not cancelable and does not bubble (use focusin when you need bubbling/delegation). Stable since about July 2015.

Discouraged as inline HTML.

MDN discourages HTML event handler attributes. Prefer el.addEventListener("focus", handler) in production; use inline onfocus for quick demos and learning. Prefer CSS :focus / :focus-visible for simple visual styles.

HTML Syntax

syntax.html
<input|button|… onfocus=" script">

script is JavaScript that runs when the element gains focus. Equivalent JS: el.onfocus = handler or el.addEventListener("focus", handler).

Attribute Values

The attribute value is always a script. Common patterns:

Value / patternDescriptionTry it
scriptAny JavaScript run when the element gains focusTry it
onfocus="fn()"Call a named function from the attributeTry it
addEventListener("focus")Preferred modern listener — e.g. update a hintTry it
With onblurShow help on focus; clear or tidy up on blurTry it

Live Preview

Click or Tab into the field. Status updates on focus and blur.

Field

Use the buttons or click the input. Tab also works.

Preview

Waiting for focus…

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — Message When Focused

Update a status line when the input receives focus.

HTML
<label for="email">Email</label>
<input id="email" type="email" name="email"
       onfocus="onFieldFocus()">
<p id="status">Click or Tab into the field.</p>

<script>
  function onFieldFocus() {
    document.getElementById("status").textContent =
      "focus — field is active.";
  }
</script>
Try It Yourself

How It Works

  1. Put onfocus on a focusable control — not on a plain non-focusable <div> (unless it has a suitable tabindex).
  2. User focuses the field (click, tap, or Tab).
  3. focus fires and runs your script.
  4. Update UI — hint text, logging, or prepare validation messaging.

Example 2 — Prefer addEventListener

Same idea with a modern listener and a short hint.

HTML
<label for="city">City</label>
<input id="city" type="text" name="city">
<p id="hint">Focus the field for a tip.</p>

<script>
  var city = document.getElementById("city");
  var hint = document.getElementById("hint");

  city.addEventListener("focus", function () {
    hint.textContent = "Tip: use your home city.";
  });
</script>
Try It Yourself

How It Works

  1. Select the input in JavaScript.
  2. Listen for focus with addEventListener.
  3. Show a tip when the field becomes active.
  4. Keep styles in CSS when you only need a visual focus ring — use JS for behavior and messages.

Example 3 — Pair with onblur

Show help on focus; clear it when focus leaves.

HTML
<label for="pw">Password</label>
<input id="pw" type="password" name="password"
       onfocus="showHelp()" onblur="hideHelp()">
<p id="help" hidden>Use at least 8 characters.</p>

<script>
  function showHelp() {
    document.getElementById("help").hidden = false;
  }
  function hideHelp() {
    document.getElementById("help").hidden = true;
  }
</script>
Try It Yourself

How It Works

  1. onfocus reveals helper text when the field is active.
  2. onblur hides it when focus moves away.
  3. hidden keeps the help out of the way until needed (and out of the accessibility tree while hidden).
  4. Classic form pattern — enter for guidance, leave to clean up or validate.

Edge Cases & Pitfalls

Most “focus never fired” bugs are listening on a non-focusable element or expecting focus to bubble.

bubble

focus does not bubble

A parent onfocus will not see child focus. Use focusin (bubbles) or addEventListener("focus", …, true) for capture/delegation.

blur

Not the same as blur

Gaining focus is focus. Losing focus is blur. Pair them for enter/leave UX.

api

Not cancelable

You cannot block focus with preventDefault() on the focus event. Manage focus with element.focus() / blur() when needed.

target

Must be focusable

Plain <div> / <span> do not receive focus unless you add an appropriate tabindex (and usually a role/keyboard plan).

css

Prefer CSS for look

For borders and rings, use :focus / :focus-visible. Use onfocus for behavior (messages, analytics), not only styling.

Browser Support

onfocus is an HTML event-handler attribute for focusable elements in the browser. It is not a Node.js, Deno, or Bun API.

✓ Baseline · Widely available

onfocus

Widely available since about July 2015. Chrome, Firefox, Safari, Edge, and Opera support focus on form controls and other focusable elements.

97% Global support (approx.)
Google Chrome All modern
Full support
Mozilla Firefox All modern
Full support
Apple Safari All modern
Full support
Microsoft Edge All modern
Full support
Opera All modern
Full support
Samsung Internet Modern
Full support
Internet Explorer 9+
Supported
onfocus Widely supported

Bottom line: Safe to use for focus UX. Prefer addEventListener('focus') in apps, CSS for visual focus, and focusin when you need bubbling.

Key Takeaways

  • Role: the element received focus.
  • Not cancelable and does not bubble.
  • Pair with onblur for enter/leave field UX.
  • Delegation: use focusin (or capture) — not bubbling focus.

One line: onfocus means the element became active — style with CSS, behave with JS, leave with onblur.

Frequently Asked Questions

It is an event-handler attribute. When a focusable element receives focus (click, tap, or Tab), the focus event fires and the JavaScript in onfocus runs.
Focusable elements such as input, textarea, select, button, a with href, and elements with a tabindex that makes them focusable.
No. Per MDN, focus does not bubble. For parent delegation, use the related focusin event (or addEventListener with capture: true).
No. Per MDN, the focus event is not cancelable. You react after focus moves — you cannot stop it with preventDefault().
onfocus runs when the element gains focus. onblur runs when it loses focus. They are often paired for help-on-enter and tidy-up-on-leave.
No. The focus event and onfocus handler are standard and Baseline widely available since about July 2015. Inline HTML handlers are discouraged in production in favor of addEventListener.

Next: onhashchange

Run JavaScript when the URL hash fragment changes.

onhashchange tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful