
Last updated: Mar 5, 2024
Reading time·3 min

Use the blur() method to remove the focus from an element, e.g.
input.blur().
If you need to remove the focus from the currently active element without
selecting it, call the blur method on the activeElement property -
document.activeElement.blur().
Here is the HTML for the examples.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>bobbyhadz.com</title> </head> <body> <input type="text" id="first_name" autofocus /> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
const input = document.getElementById('first_name'); // ✅ Remove focus from specific element input.blur(); // ✅ Remove focus from the currently active element // document.activeElement.blur();
We selected the input element using the document.getElementById() method.
We used the HTMLElement.blur() method to remove the focus from the element.
blur() method removes the keyboard focus from the element it was called on.If you want to remove the focus from the currently active element without
selecting it, call the blur() method on the document.activeElement property.
// ✅ Remove focus from the currently active element document.activeElement.blur();
The document.activeElement property returns the element that currently has focus.
document.activeElement property will return the body element in most browsers, but it could also return null depending on the browser's implementation.To avoid getting an error due to calling the blur() method on a null value,
use the optional chaining (?.) operator.
document.activeElement?.blur();
document.activeElement property returns null, the optional chaining operator will short-circuit returning undefined instead of calling the blur() method.If you need to focus a different element, you can use the focus() method.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>bobbyhadz.com</title> </head> <body> <input type="text" id="first_name" autofocus /> <input type="text" id="country" placeholder="Country" /> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
const countryInput = document.getElementById('country'); countryInput.focus();
Even though the input element with id of first_name has the autofocus
attribute set, we are able to change the focused element by calling the
focus() method on a different element.
To disable focus on a specific element, set the tabIndex property on the
element to a value of -1.
The tabIndex property can be used to specify that an element cannot be focused
using keyboard navigation.
Here is the HTML for the examples.
<!DOCTYPE html> <html lang="en"> <head> <title>bobbyhadz.com</title> <meta charset="UTF-8" /> </head> <body> <input type="text" id="first_name" name="first_name" /> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
const element = document.getElementById('first_name'); element.tabIndex = '-1';
We set the element's
tabindex
to a value of -1.
If you need to remove focus from a specific element, you can use the blur() method.
const element = document.getElementById('first_name'); element.blur();
The method removes keyboard focus from the element it was invoked on.
If you need to make an element unclickable, set its disabled attribute.
const element = document.getElementById('first_name'); element.setAttribute('disabled', '');
When the element's disabled attribute is set, the element is not mutable or
focusable.
The setAttribute() method sets the supplied attribute to the specified value.
The method takes the following 2 parameters:
name - the name of the attribute to be set.value - the value to assign to the attribute.You can learn more about the related topics by checking out the following tutorials: