# Focusing: focus/blur An element receives a focus when the user either clicks on it or uses the `key:Tab` key on the keyboard. There's also an `autofocus` HTML attribute that puts the focus into an element by default when a page loads and other means of getting a focus. Focusing generally means: "prepare to accept the data here", so that's the moment when we can run the code to initialize or load something. The moment of losing the focus ("blur") can be even more important. That's when a user clicks somewhere else or presses `key:Tab` to go to the next form field, or there are other means as well. Losing the focus generally means: "the data has been entered", so we can run the code to check it or even to save it to the server and so on. There are important peculiarities when working with focus events. We'll do the best to cover them here. [cut] ## Events focus/blur The `focus` event is called on focusing, and `blur` -- when the element loses the focus. Let's use them for validation of an input field. In the example below: - The `blur` handler checks if the field the email is entered, and if not -- shows an error. - The `focus` handler hides the error message (on `blur` it will be checked again): ```html run autorun height=60 Your email please:
``` Modern HTML allows to do many validations using input attributes: `required`, `pattern` and so on. And sometimes they are just what we need. JavaScript can be used when we want more flexibility. Also we could automatically send the changed value on the server if it's correct. ## Methods focus/blur Methods `elem.focus()` and `elem.blur()` set/unset the focus on the element. For instance, let's make the visitor unable to leave the input if the value is invalid: ```html run autorun height=80 Your email please: ``` It works in all browsers except Firefox ([bug](https://bugzilla.mozilla.org/show_bug.cgi?id=53579)). If we enter something into the input and then try to use `key:Tab` or click away from the ``, then `onblur` returns the focus back. Please note that we can't "prevent losing focus" by calling `event.preventDefault()` in `onblur`, because `onblur` works *after* the element lost the focus. ```warn header="JavaScript-initiated focus loss" A focus loss can occur for many reasons. One of them is when the visitor clicks somewhere else. But also JavaScript itself may cause it, for instance: - An `alert` moves focus to itself, so it causes the focus loss at the element (`blur` event), and when the `alert` is dismissed, the focus comes back (`focus` event). - If an element is removed from DOM, then it also causes the focus loss. If it is reinserted later, then the focus doesn't return. These features sometimes cause `focus/blur` handlers to misbehave -- to trigger when they are not needed. The best recipe is to be careful when using these events. If we want to track user-initiated focus-loss, then we should evade causing it by ourselves. ``` ## Allow focusing on any element: tabindex By default many elements do not support focusing. The list varies between browsers, but one thing is always correct: `focus/blur` support is guaranteed for elements that a visitor can interact with: `