×

JavaScript focus on input

In JavaScript, the elements can be focused using either focus() method or onfocus() event.

What is focus() method?

When we want to focus on an element (if it can be focused) or basically highlight it, the focus() method is used.

This method is supported by mostly all the browsers like Google chrome, safari, Microsoft Edge/Internet Explorer, Oracle, Firefox etc.

NOTE: This method takes no parameters and has no return value as well.

Syntax:

element.focus()

Example:

<!DOCTYPE html>
<html>
<body>


Enter text here: <input type="text" id="tcontent">


<p>This is the example to show a how user can use focus method to an input element.</p>


<button type="button" onclick="myFunctiondemo()">Focus</button>


<script>
function myFunctiondemo() {
  document.getElementById("tcontent").focus();
}
</script>
</body>
</html>

Output:

Intially,

JavaScript Focus On Input

After the button has been selected, the text box gets highlighted

JavaScript Focus On Input

What is onfocus() event?

Whenever an element gets focus, the onfocus() event occurs.
The <input>, <select> and <a> use the onfocus() event commonly.
The onfocus() event can be used with all HTML tags expect for: <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style>, and <title> tags.
Similar to focus() method, the onfocus event is also supported by mostly all the browsers like Google chrome, safari, Microsoft Edge/Internet Explorer, Oracle, Firefox etc.

The input element and onfocus event

An input field is defined by the input tag where the user can enter data. The input tag element can be used to display several types of properties like radio buttons, text fields, checkboxes, dates, email Ids, passwords etc.

So, the onfocus() event or the focus method helps to set focus on the specified input element. The focused element helps in determining that which element will receive keyboard inputs and similar events by default.

The onfocus event can be used commonly in three ways:

  • In HTML,

Syntax:

<element onfocus= functionname>


<script>
function functionname(){
	//definition of the function
}
</script>

Example:

<!DOCTYPE html>
<html>
<body>


<p>This is the example to show a user can use "onfocus" event to an input element.</p>


Enter text here: <input type="text" id="tcontent" onfocus="myFunctiondemo()">


<script>
function myFunctiondemo() {
  document.getElementById("tcontent").style.backgroundColor = "lightblue";
}
</script>


</body>
</html>

Output:

Initially, the input element looks like this,

JavaScript Focus On Input

On focusing (selecting the input text-box):

JavaScript Focus On Input
  • In Javascript

Syntax:

<script>
object.onfocus = function(){functionname};


function functionname(){
	//definition of the function
}
</script>

Example:

<!DOCTYPE html>
<html>
<body>


<p>This is the example to show a user can use "onfocus" event to an input element.</p>


Enter text here: <input type="text" id="tcontent">


<script>
document.getElementById("tcontent").onfocus = function() {myFunctiondemo()};


function myFunctiondemo() {
  document.getElementById("tcontent").style.backgroundColor = "pink";
}
</script>


</body>
</html>

Output:

After the input element is focused (the text box is selected),

JavaScript Focus On Input
  • Using addEventListener() method in JavaScript

Syntax:

<script>
object.addEventListener("focus", functionname);
function functionname(){
	//definition of the function
}
</script>

Example:

<!DOCTYPE html>
<html>
<body>


<p>This is the example to show a user can use "onfocus" event to an input element.</p>


Enter text here: <input type="text" id="tcontent">


<script>
document.getElementById("tcontent").addEventListener("focus", myFunctiondemo);


function myFunctiondemo() {
  document.getElementById("tcontent").style.backgroundColor = "yellow";
}
</script>


</body>
</html>

Output:

After the input element is focused (the text box is selected),

JavaScript Focus On Input

NOTE: Internet Explorers and older versions do not support the method of addEventListener().

How to remove focus from the element?

  • By using the blur() method

Syntax:

element.blur()

Example:

<!DOCTYPE html>
<html>
<body>


Enter text here: <input type="text" id="tcontent">


<p>This is the example to show how a user can use blur method to an input element.</p>




<button type="button" onclick="myFunctiondemo()">Lose focus</button>


<script>


function myFunctiondemo() {
  document.getElementById("tcontent").blur();
}
</script>


</body>
</html>

Output:

When the text box is highlighted initially,

JavaScript Focus On Input

After the button has been selected, the text box loses focus

JavaScript Focus On Input
  • By using the onblur() event

Syntax:

<element onblur= “functionname()”>

Example:

<!DOCTYPE html>
<html>
<body>


<p>This is the example to show a user can use "onblur" event to an input element.</p>


Enter text here: <input type="text" id="tcontent" onblur="myFunctiondemo()">


<script>
function myFunctiondemo() {
  alert("Input field has lost focus.");
}
</script>


</body>
</html>

Output:

After the data has been entered in the input element, if the user clicks anywhere outside the field, it loses focus and an alert appears,

JavaScript Focus On Input

Related Topics

Javascript Email Validation

We can validate the email with the help of JavaScript. Here we check the condition related to any email id, like email it must have "@" and "." sign and...

2 minutes read.

Javascript Redirect Button

What is a redirect button? When a button acts as a hyperlink, it is known as a redirect button. On clicking the button, it transfers the user to another page. How to...

3 minutes read.

Javascript comment

Introduction In programming languages, comments are a method to include meaningful annotations within the code that the compiler or interpreter will ignore at run time. That is to say, though comments...

7 minutes read.

Javascript getElementByID

The document.getElementById() method returns the element of specified id. In below example we receive input field by using document.getElementById() method, here document.getElementById() receive input value on the basis of input field...

1 minute read.

Javascript loops

Loops in JavaScript: Loops are used to execute a specific piece or block of code repeatedly until the given condition is satisfied. What is a Loop? Almost all loops contain a certain...

7 minutes read.

Javascript Date

In JavaScript Date Object is data type build into the JavaScript language. Data object are created with the new Date(). JavaScript Date instance that represents a single moment in time. JavaScript...

2 minutes read.

JavaScript setTimeout()

The setTimeout() method creates a timer that, when it expires, runs a function or provides a piece of code. setTimeout() is an asynchronous function, which means that it will not interrupt...

3 minutes read.

Functional Programming in JavaScript

Most developers work with object-oriented programming while developing their applications, but sometimes they need to change their approach to solve some specific tasks. Although mostly we tend to go for...

6 minutes read.

JavaScript radio button checked value

What are radio buttons? A radio button is defined as an icon that is used to take input from the user. The user can choose only one option(value) from the group...

5 minutes read.

Javascript Switch Statement

Introduction Conditional statements are simple building blocks in programming languages and are utilized fairly frequently. This portion of Developing Conditional Statements in JavaScript talks about how to utilize the if, else,...

5 minutes read.

Javascript Find Object In Array

What is find() method? It returns the value of the first element in the given array that fulfils the given testing function. However, if no values from the array are able...

3 minutes read.

JavaScript focus on input

In JavaScript, the elements can be focused using either focus() method or onfocus() event. What is focus() method? When we want to focus on an element (if it can be focused) or...

4 minutes read.

Javascript Object

An object is an entity having state and behavior. JavaScript is an object oriented scripting language. JavaScript is template based not class based but we can create object directly. Syntax to...

2 minutes read.

Javascript Dialog Box

A JavaScript dialog box is predefined function which is used to perform different task. Some functions are used in JavaScript Dialog box. FunctionDescriptionalert()To give alert message to userprompt()To input value from...

2 minutes read.

JavaScript dropdown onchange

What is onchange event? It is an event in JavaScript used to make the web pages dynamic. The onchange event gets triggered whenever the value of an event changes. It usually...

3 minutes read.

JavaScript moment date difference

Often date differences are required on the web platforms for various reasons. They can be: To find the duration, of course, someone is pursuingTo find the age of a user from...

7 minutes read.

Javascript Page Redirect

The window.location object can be used to get the current page address (URL) and to redirect browser to new page. In some websites when we visit, we face a situation where we...

3 minutes read.

D3.js Tutorial

Introduction of D3.js D3.js is referred to as a JavaScript library that is used to manipulate the documents according to the data. The intensity of D3 is based on web standards...

5 minutes read.

Synchronous and Asynchronous in JavaScript

JavaScript is a powerful and versatile language that is used all over the web, from small websites to large applications. It is essential for developers to understand the differences between...

9 minutes read.

Design a BMI calculator using JavaScript

BMI calculator BMI stands for Body Mass Indicator. It is a numeric value which is calculated based on the height and weight of a person. It represents the fatness of the...

4 minutes read.