Focusing - focus/blur
Forms / Controls: Focusing - focus/blur
What is form focus in JavaScript?
View Answer:
Can you explain the 'focus' event?
View Answer:
How do you set focus on an element in JavaScript?
View Answer:
Here's an example of how you might set focus on an input element using JavaScript:
// Assume there is an input element with id 'myInput'
var inputElement = document.getElementById('myInput');
// Set focus on the input element
inputElement.focus();
In this example, we first get a reference to an input element using document.getElementById()
. We then call focus()
on this element, which will cause the browser to give it focus.
How do you remove focus from an element in JavaScript?
View Answer:
Here's an example of how you might remove focus from an input element using JavaScript:
// Assume there is an input element with id 'myInput'
var inputElement = document.getElementById('myInput');
// Remove focus from the input element
inputElement.blur();
In this example, we first get a reference to an input element using document.getElementById()
. We then call blur()
on this element, which will cause the browser to remove focus from it.
What are the 'focusin' and 'focusout' events?
View Answer:
What is the difference between 'focus/blur' and 'focusin/focusout' events?
View Answer:
What is tab index and how does it relate to form focus?
View Answer:
Here's a simple example of how you might use the tabindex
attribute in a form:
<form>
<input type="text" name="first" tabindex="2">
<input type="text" name="second" tabindex="1">
</form>
In this example, despite being first in the source order, the "first" input field will receive focus after the "second" field when the user navigates through the form using the Tab key. This is because "second" has a lower tabindex
.
Note: Use tabindex
sparingly and wisely, because overuse can lead to navigation issues, particularly for keyboard-only and screen reader users. It's generally best to rely on the default tab order unless there's a compelling reason not to.
How can we prevent a form element from getting focus?
View Answer:
What's the use of the 'autofocus' attribute in forms?
View Answer:
What's the use of the 'autofocus' attribute in forms?
View Answer:
What does it mean to focus on an element?
View Answer:
How does blur work in relation to a focus event on an element?
View Answer:
Can you explain how focus and blur events work in JavaScript?
View Answer:
<style>
.invalid {
border-color: red;
}
#error {
color: red;
}
</style>
Your email please: <input type="email" id="input" />
<div id="error"></div>
<script>
input.onblur = function () {
if (!input.value.includes('@')) {
// not email
input.classList.add('invalid');
error.innerHTML = 'Please enter a correct email.';
}
};
input.onfocus = function () {
if (this.classList.contains('invalid')) {
// remove the "error" indication, because the user wants to re-enter something
this.classList.remove('invalid');
error.innerHTML = '';
}
};
</script>
What is the cause of JavaScript-initiated focus loss?
View Answer:
Is there a way to focus/blur on any element in the DOM?
View Answer:
<!-- Click the first item and press Tab. -->
<ul>
<li tabindex="1">One</li>
<li tabindex="0">Zero</li>
<li tabindex="2">Two</li>
<li tabindex="-1">Minus one</li>
</ul>
<style>
li {
cursor: pointer;
}
:focus {
outline: 1px dashed green;
}
</style>
Is there a way to focus/blur and ensure bubbling happens?
View Answer:
<form id="form">
<input type="text" name="name" value="Name" />
<input type="text" name="surname" value="Surname" />
</form>
<style>
.focused {
outline: 1px solid red;
}
</style>
<script>
form.addEventListener('focusin', () => form.classList.add('focused'));
form.addEventListener('focusout', () => form.classList.remove('focused'));
</script>
We must assign them with elem.addEventListener rather than on‹event›.