JavaScript - blur() method

revision:


Category : element

The HTMLElement.blur() method removes keyboard focus from the current element.

The blur() method removes focus from an element.

Syntax :

        HTMLElementObject.blur()
    

Parameters : none

Examples:

            const input = document.getElementById('first_name');
            // Remove focus from specific element
            input.blur();
            // Remove focus from the currently active element
            document.activeElement.blur();

        

Practical examples

example: click the buttons to give or remove focus from the text field
code:
                    <div>
                        <input type="text" id="myText" value="A text field">
                        <button type="button" onclick="getFocus()">Get focus</button>
                        <button type="button" onclick="loseFocus()">Lose focus</button>
            
                    </div>
                    <script>
                        function getFocus() {
                        document.getElementById("myText").focus();
                        }
            
                        function loseFocus() {
                        document.getElementById("myText").blur();
                        }
                        
                    </script>
                

example: ckick the buttons to give or remove focus from the link
Visit my website at lwitters.com

Click the buttons to give or remove focus from the link above.

code:
                    <div>
                        <a id="myAnchor" href="https://www.lwitters.com">Visit my website at lwitters.com</a>
                        <p>Click the buttons to give or remove focus from the link above.</p>
                        <input type="button" onclick="getfocus2()" value="Get focus">
                        <input type="button" onclick="losefocus2()" value="Lose focus">
                    </div>
                    <style>
                        a:focus, a:active {color: red;}
                    </style>
                    <script>
                        function getfocus2() {
                            document.getElementById("myAnchor").focus();
                        }
                        function losefocus2() {
                            document.getElementById("myAnchor").blur();
                        }
                    </script>
                

example: remove focus from a text input; the input will lose focus after 3 seconds


code:
                    <div>
                        <input type="text" id="sampleText" value="Sample Text" /> <br /><br />
                        <button type="button" onclick="focusInput()">Click me to gain focus</button>
                    </div>
                    <script>
                        function focusInput() {
                            const textField = document.getElementById("sampleText");
                            textField.focus();
                            // The input will lose focus after 3 seconds
                            setTimeout(() => {
                                textField.blur();
                            }, 3000);
                        }
                    </script>