
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How does JavaScript focus() method works?
focus()
Javascript focus() methods helps to highlight a HTML form element. It sets the element as an active element in the current document. In current documentation, focus can be applied to only one single element. The focus can be applied either to a text, a button, etc.
syntax
element.focus(options);
Example
In the following example, initially, the text "Tutorialspoint" is in an anchor tag 'a', which as an id called 'focus'. Later on, a function is declared in which the id is called using a DOM method and focus() method is applied on it to change the color of text "Tutorialspoint" to red. Here a button is used for the click event.
<html> <head> <style> a:focus, a:active { color: red; } </style> </head> <body> <a href="http://www.tutorialspoint.com/" id="focus">Tutorialspoint</a> <input type="button" value="getfocus" onclick="getfo()"> <script> function getfo() { document.getElementById("focus").focus(); } </script> </body> </html>
After executing the above code we will get the following image displayed on the screen.
Once we click on the get focus button we will get the following output.
Output
- Related Articles
- How does digital thermometer works?
- How does jQuery event namespace works?
- How does Secure Hash Algorithm works?
- How does the MD5 Algorithm works?
- How does == operator works in Python 3?
- How does Constraint Layout works in android?
- How does DES works in Information Security?
- How does Steganography works in Information Security?
- How does Hibernate Second Level Cache Works?
- How "getElementByID" works in JavaScript?
- How does the pandas series.ge() method works if the series object contains string type elements?
- JavaScript focus
- How does MYSQL control flow function CASE works?
- How does the java "for each" loop works
- How does Promise.all() method differs from Promise.allSettled() method in JavaScript?
