
- HTML Tutorial
- HTML - Home
- HTML - Overview
- HTML - Basic Tags
- HTML - Elements
- HTML - Attributes
- HTML - Formatting
- HTML - Phrase Tags
- HTML - Meta Tags
- HTML - Comments
- HTML - Images
- HTML - Tables
- HTML - Lists
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML - Frames
- HTML - Iframes
- HTML - Blocks
- HTML - Backgrounds
- HTML - Colors
- HTML - Fonts
- HTML - Forms
- HTML - Embed Multimedia
- HTML - Marquees
- HTML - Header
- HTML - Style Sheet
- HTML - Javascript
- HTML - Layouts
- HTML References
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Entities
- HTML - Fonts Ref
- HTML - Events Ref
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
HTML DOM blur() Method
The HTML DOM blur() method is used to remove the keyboard focus from a specific element. Using blur we can add blur or remove blur to any HTML element. The blur() method can help in better navigation around the webpage as we can specifically focus on an element based on the user input.
Syntax
Following is the syntax for blur() method −
Object.blur()
Example
Let us see an example of the blur() method −
<!DOCTYPE html> <html> <head> <style> a{ text-decoration:none; font-size:20px; } a:focus, a:active { color: red; text-decoration:underline; font-size:40px; } </style> </head> <body> <a id="Anchor" href="https://example.com">example.com</a> <p>Give focus or remove focus…</p> <input type="button" onclick="getfocus()" value="Get focus"> <input type="button" onclick="losefocus()" value="Lose focus"> <script> function getfocus() { document.getElementById("Anchor").focus(); } function losefocus() { document.getElementById("Anchor").blur(); } </script> </body> </html>
Output
This will produce the following output −
On clicking “Get focus” −
On clicking “Lose focus”, it will be the same as original −
In the above example −
We have first created an anchor tag with id “Anchor” and href=”https://www.example.com” −
<a id="Anchor" href="https://example.com">example.com</a>
We have then defined two styles for the anchor tag to distinguish it when it is focused and active and when it is not.
a{ text-decoration:none; font-size:20px; } a:focus, a:active { color: red; text-decoration:underline; font-size:40px; }
We have then created two buttons “Get focus” and “Lose focus” to execute getfocus() and losefocus() functions respectively.
<input type="button" onclick="getfocus()" value="Get focus"> <input type="button" onclick="losefocus()" value="Lose focus">
The getfocus() function gets the element which has “Anchor” id associated with it which in our case is <anchor> element.It then executes it focus method which will change the links to the a:focus,a:active style i.e the color will be green, text will be underlined and font size increased to 40px.
function getfocus() { document.getElementById("Anchor").focus(); }
The losefocus() function gets the element which has “Anchor” id associated with it which is the <anchor> element in our case. It then executes its blur() method to loose focus from the above link and changes the link style to the original style for <a> element.
function losefocus() { document.getElementById("Anchor").blur(); }
- Related Articles
- HTML DOM addEventListener() Method
- HTML DOM normalize( ) Method
- HTML DOM write() Method
- HTML DOM writeln() Method
- HTML DOM insertAdjacentElement( ) Method
- HTML DOM insertAdjacentHTML( ) Method
- HTML DOM insertAdjacentText( ) Method
- HTML DOM insertBefore( ) Method
- HTML DOM isDefaultNamespace( ) Method
- HTML DOM isEqualNode( ) Method
- HTML DOM isSameNode( ) Method
- HTML DOM item( ) Method
- HTML DOM appendChild() Method
- HTML DOM click() method
- HTML DOM cloneNode() method
