👀 Live Preview
Click or Tab into the field — focus adds a blue outline:
Focus the input to see the handler run.

The onfocus attribute is an inline event handler that runs JavaScript when the focus event fires. That happens when a focusable element receives focus — by mouse click, tap, or keyboard Tab. Common targets include <input>, <textarea>, <select>, <button>, and links. Use it to highlight the active field, show helper text, or prepare validation UI. Pair it with onblur for enter-and-leave flows. The focus event does not bubble; use focusin for delegation on a parent.
Inline JS.
Gain focus.
input, button.
Enter / leave.
Preferred way.
Keyboard nav.
onfocus AttributeThe primary purpose of onfocus is to respond when a user starts interacting with a control. You might highlight the active input, reveal formatting hints, or fetch suggestions for an autocomplete field. Because focus also moves via keyboard, onfocus helps keyboard users get the same feedback as mouse users.
Keep handlers lightweight — heavy work on every focus can feel sluggish. For validation, many apps validate on blur (leave) rather than on every focus, but showing hints on focus is a common pattern.
Use input.addEventListener("focus", handler) instead of inline onfocus when building maintainable forms. CSS :focus and :focus-visible often handle visual states without JavaScript.
Set onfocus on a focusable element, or assign element.onfocus:
<script>
function highlightInput(el) {
el.style.outline = "2px solid #2563eb";
}
</script>
<input type="text" id="name" onfocus="highlightInput(this)">focus event fires.input, textarea, select, button, a[href], [tabindex].element.focus().element.onfocus = function() { … }.element.addEventListener("focus", handler).focus does not bubble — use focusin for parent delegation.The onfocus attribute accepts a string of JavaScript code:
onfocus="highlightInput()" — Call a named function.onfocus="showHint()" — Inline statement.field.onfocus = () => { … } assigns the handler.const email = document.getElementById("email");
email.addEventListener("focus", () => {
document.getElementById("email-hint").hidden = false;
});
email.addEventListener("blur", () => {
document.getElementById("email-hint").hidden = true;
});| Action | focus fires? | Notes |
|---|---|---|
| Click into input | Yes | Most common trigger |
| Tab to next field | Yes | Keyboard navigation |
| Click outside field | No (blur fires) | Use onblur |
element.focus() in JS | Yes | Programmatic focus |
| Handler attribute | onfocus | Inline on element |
| Bubbles to parent? | No | Use focusin |
| Target | Supported? | Notes |
|---|---|---|
<input> | Yes | Most common use |
<textarea>, <select> | Yes | Form controls |
<button>, <a href> | Yes | Interactive elements |
tabindex="0" on div | Yes | Custom focusable regions |
<div> without tabindex | No | Not focusable by default |
onfocus vs onblur vs onclick| Handler | When it fires | Typical use |
|---|---|---|
onfocus | Element gains focus | Highlight field, show hint |
onblur | Element loses focus | Validate input on leave |
onclick | Pointer activation | Button actions, toggles |
Inline highlight on focus, addEventListener, and pairing with onblur.
Click or Tab into the field — focus adds a blue outline:
Focus the input to see the handler run.
Change border style when the input receives focus:
<input type="text" id="myInput" onfocus="highlightInput()">
<script>
function highlightInput() {
document.getElementById("myInput").style.border = "2px solid #2563eb";
}
</script>The browser fires focus when the field becomes active. The inline onfocus attribute wires up the handler.
Attach the handler with addEventListener:
<script>
document.getElementById("dynamicInput").addEventListener("focus", function () {
console.log("Input is in focus!");
});
// Or property form:
document.getElementById("dynamicInput").onfocus = function () { /* … */ };
</script>Register the listener once at page load. Focus from mouse or keyboard triggers the same handler.
Show a hint on focus and hide it on blur:
const field = document.getElementById("email");
const hint = document.getElementById("email-hint");
field.addEventListener("focus", () => {
hint.hidden = false;
});
field.addEventListener("blur", () => {
hint.hidden = true;
});Focus and blur are complementary events on the same element — ideal for contextual hints without cluttering the default view.
:focus-visible — Show focus rings for keyboard users without noisy outlines on every click.<label for> so focus moves predictably when the label is clicked.tabindex values that confuse navigation.Click, tap, or Tab key.
Browser moves focus.
onfocus handler runs.
Hints, highlight, prep work.
The focus event and onfocus handler are supported in all modern browsers on focusable elements — Chrome, Firefox, Safari, and Edge.
onfocus supportAll major browsers fire the underlying event and honor the onfocus handler attribute.
Bottom line: Use onfocus confidently on form controls; prefer addEventListener and CSS focus styles in production.
onblur for enter-and-leave field behavioraddEventListener("focus", …) in production code:focus-visible when possiblealert() when a field receives focusfocus bubbles (use focusin for delegation)onfocus to non-focusable elementsThe onfocus attribute runs JavaScript when a focusable element becomes active — essential for form hints, highlights, and accessible keyboard flows.
Prefer addEventListener("focus", …), pair with onblur, and use CSS for visual focus states whenever you can.
onfocusBookmark these before building your next interactive form.
focus fires.
Eventinput, button.
ScopeKeyboard too.
A11yEnter / leave.
PatternFor bubbling.
Delegatefocus event fires — when a focusable element receives focus.input, textarea, select, button, a[href], and elements with tabindex.onfocus runs when focus enters. onblur runs when focus leaves. They are often used together.focusin with addEventListener if you need delegation on a parent element.addEventListener("focus", handler) is preferred for production. CSS :focus-visible handles many styling needs without JS.Practice the onfocus attribute with highlight and hint examples in the Try It editor.
5 people found this page helpful