![]() If it is reinserted later, then the focus doesn’t return. If an element is removed from DOM, then it also causes the focus loss.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).But also JavaScript itself may cause it, for instance: One of them is when the visitor clicks somewhere else. The focus handler hides the error message (on blur it will be checked again):.The blur handler checks if the field has an email entered, and if not – shows an error.Let’s use them for validation of an input field. The focus event is called on focusing, and blur – when the element loses the focus. We’ll do the best to cover them further on. There are important peculiarities when working with focus events. 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. That’s when a user clicks somewhere else or presses Tab to go to the next form field, or there are other means as well. The moment of losing the focus (“blur”) can be even more important. There’s also an autofocus HTML attribute that puts the focus onto an element by default when a page loads and other means of getting the focus.įocusing on an element generally means: “prepare to accept the data here”, so that’s the moment when we can run the code to initialize the required functionality. An element receives the focus when the user either clicks on it or uses the Tab key on the keyboard.
0 Comments
Leave a Reply. |