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.
Syntax
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).
Values
Attribute Values
The attribute value is always a script. Common patterns:
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…
Code
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>
Put onfocus on a focusable control — not on a plain non-focusable <div> (unless it has a suitable tabindex).
User focuses the field (click, tap, or Tab).
focus fires and runs your script.
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>
hidden keeps the help out of the way until needed (and out of the accessibility tree while hidden).
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.
Compatibility
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 ChromeAll modern
Full support
Mozilla FirefoxAll modern
Full support
Apple SafariAll modern
Full support
Microsoft EdgeAll modern
Full support
OperaAll modern
Full support
Samsung InternetModern
Full support
Internet Explorer9+
Supported
onfocusWidely supported
Bottom line: Safe to use for focus UX. Prefer addEventListener('focus') in apps, CSS for visual focus, and focusin when you need bubbling.
Remember
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.