This blog will explain the procedure to remove the focus from an element using JavaScript.
How to Remove the Focus From an Element in JavaScript?
To remove the focus from an element in JavaScript, apply the following approaches in combination with the “blur()” method:
Approach 1: Remove the Focus From an Element in JavaScript Using getElementById() Method
The “blur()” method removes the focus from the associated element, and the “getElementById()” method returns an element having the specified “id”. These methods can be applied in combination to fetch the focused element and remove the focus from it with the help of the user-defined function.
Syntax
In the given syntax:
“element” corresponds to the element that needs to be fetched against the particular “id”.
Example
Let’s overview the following example:
<input type="radio" id="head" autofocus> This is a webpage
<br><br>
<button onclick="removeFocus()">Click Me</button>
</center></body>
<script type="text/javascript">
function removeFocus(){
const input = document.getElementById('head');
input.blur();
}
</script>
In the above lines of code:
- Include an “<input>” element having the stated attributes.
- The “type” attribute signifies that the element is a “radio” button. The “autofocus” is a boolean attribute that adds focus to the associated element.
- In the next step, create a button having an “onclick” event which will redirect to the function removeFocus().
- In the JS code, define a function named “removeFocus()”. In the function definition, access the contained element by its “id” using the “getElementById()” method.
- Lastly, apply the “blur()” method to the fetched element. This will resultantly remove the focus from the <input> element upon the button click.
Output
In the output, it can be seen that the focus from the radio button is omitted upon the button click.
Approach 2: Remove the Focus From an Element in JavaScript Using activeElement Property and optional chaining(?.) Operator
The “activeElement” property gives the HTML element that has focus, and the “optional chaining(?.)” operator checks for a particular condition. These approaches can be utilized in combination to apply a check upon the focused element(s) and blur them accordingly.
Example
Let’s go through the below-stated example:
<input type= "checkbox" >Python
<br><br>
<input type= "checkbox" autofocus>JavaScript
<br><br>
<button onclick="removeFocus()" >Click the button to remove focus</button>
<br><br>
</center></body>
<script type="text/javascript">
function removeFocus(){
document.activeElement?.blur();
}
</script>
In the above code snippet:
- Include two “<input>” elements having the allocated attribute “type” as a “checkbox”.
- The boolean attribute “autofocus” is allocated to the latter checkbox, as stated.
- Next, create a button having an “onclick” event accessing the function named removeFocus().
- In the JS code part, define a function named “removeFocus()”.
- In its definition, apply the combined “activeElement” property and the “optional chaining(?.)” operator to check for all of the focused element(s) within the code.
- The associated “blur()” method will blur the located focused element(s) upon the button click.
Output
In the output, the focus from the stated checkbox is removed upon the click of the button.
Conclusion
The “blur()” method combined with the “getElementById()” method or the “activeElement” property and “optional chaining(?.)” operator can be utilized to remove/omit the focus from an element in JavaScript. The former approach can be applied to get the focused element and remove the focus from it upon the button click. The latter approach can be utilized to check the focused element(s) and blur it. This write-up explains how to remove/omit focus from an element in JavaScript.