JavaScript Focus()

Learn via video course
FREE
View all courses
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
Topics Covered

Overview

A HTML form element can be highlighted using Javascript focus() method. The focus in javascript aims to move the application window to the foreground in order to set an element as the active element. Note that sometimes JavaScript's security settings prevent that from happening.

While the focus in javascript attempts to bring the application window to the foreground and activate an element, the setActive() method for Internet Explorer does not achieve this.

Syntax of JavaScript focus()

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

What is JavaScript focus() Method?

The focus in javascript can be used to focus an HTML element. It designates the element as the one that is now active in the document. In the current document, it can be used on a single HTML element. The element could be a window, text box, button, etc. All browsers are compatible with it.

Parameters of JavaScript focus()

options : An optional object that offers choices for controlling different focusing-related processes.

Property of objectDescription
preventScrolla Boolean value indicating whether or not the document should be scrolled to display the newly-focused element. PreventScroll's default value of false instructs the browser to scroll the element into view after it has been focused. No scrolling will take place if preventScroll is set to true.

Return Value of JavaScript focus()

focus in javascript method has no return value.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Exceptions of JavaScript focus()

To prevent the focus from departing the HTMLElement. event.preventDefault() must be used if HTMLElement.focus() is used from a mousedown event handler.

Example

Focus on a Text Field

Output:

Before Clicking on the Button:

TEXT FIELD BEFORE CLICKING

After Clicking on the Button:

TEXT FIELD AFTER CLICKING

More Examples

Focus on a link

Before Clicking on the Button:

LINK BEFORE CLICKING

After Clicking on the Button:

LINK AFTER CLICKING

Focus on a Text Field and remove focus from the Text Field

Before Clicking on the Button:

REMOVE FOCUS BEFORE CLICKING

After Clicking on the focus Button:

REMOVE FOCUS AFTER CLICKING

After Clicking on the remove focus Button:

REMOVE FOCUS AFTER REMOVAL

In the above code, we use blur() to remove focus from the element.

Supported Browser

BrowserVersion
Chrome1 and higher
Edge12 and higher
Firefox1 and higher
Internet Explorer3 and higher
Opera3 and higher
Safari1 and higher

Conclusion

  • The focus and activation-related events are triggered on the element when the focus() method is called.
  • Although an element with focus is usually the active element in the page, an active element is not required to have focus.
  • The element is made the active element in the current document via the focus() method. In a document, only one element can be active at once.
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more