JavaScript

How to Press Enter Key Programmatically in JavaScript

While going through the internet, we come across websites where it is required to fill a particular form. In such cases, pressing enter key programmatically in JavaScript is very helpful to cope with the entered incomplete information beforehand. Before proceeding to the next stage, this feature can also assist in filling the fields.

This manual will discuss the methods to programmatically hit the Enter key in JavaScript.

How to Press Enter Key Programmatically in JavaScript?

To press enter key programmatically using JavaScript, the following approaches can be utilized:

    • document.addEventListener()” method
    • document.querySelector()” method
    • document.getElementById()” method

Go through the mentioned methods one by one!

Method 1: Press Enter Key Programmatically in JavaScript Using document.addEventListener() Method

The “document.addEventListener()” method merges an event handler to a document. This method can be implemented to add the specified event to detect whenever the enter key is pressed.

Syntax

document.addEventListener(event, function)

 
In the above syntax, the term “event” refers to the event that will invoke the specified “function” when it gets triggered.

The idea stated above is shown in the example below.

Example

In the following example, we will apply the “document.addEventListener()” method and add an event named “keydown”. This will result in notifying the user when the “Enter” key is pressed via alert:

document.addEventListener("keydown", (e) => {
if (e.key == "Enter"){
 alert("Enter Key is Pressed")
 }
});

 
The corresponding output will be:

Method 2: Press Enter Key Programmatically in JavaScript Using document.querySelector() Method

The “document.querySelector()” method gets the first element that a CSS selector matches. This method can be utilized to access a particular element, change its value and display it on the Document Object Model(DOM) when the Enter key is pressed.

Syntax

document.querySelector(CSS selectors)

 
Here, “CSS selectors” refers to one or more than one CSS selectors.

Look at the following example.

Example

First, we will add the following heading using the “<h1>” tag:

<h1>Result</h1>

 
Next, apply the “document.querySelector()” method to access the specified heading:

let enterKey = document.querySelector("h1");

 
Now, attach an event named “keydown” using the “document.addEventListener()” method discussed in the previous method. Here, also place a condition for the “Enter” key that check if the Enter key is pressed:

document.addEventListener("keydown", (e) => {
 if (e.key == "Enter") {
  enterKey.innerText = "Enter Key is Pressed";
  }
});

 
Output


In the above output, it can be observed that the “innerText” property changes the DOM text upon pressing the “Enter” key.

Method 3: Press Enter Key Programmatically in JavaScript Using document.getElementById() Method

The “document.getElementById()” method accesses an element with the specified id. This method can be utilized to identify the Enter key when a user enters text in the input field.

Syntax

document.getElementById(elementID)

 
In the above-mentioned syntax, “elementID” represents the id of the element which we want to access.

Example

Firstly, include a heading using the “<h3>” tag:

<h3>Fill the input field</h3>

 
In the next step, create an input field for entering the text with the following “id” and “placeholder” values:

<input type= "text" id= "input" placeholder= "Enter some text">

 
Next, fetch the assigned id using the “document.getElementById()” method:

let enterKey= document.getElementById("input")

 
Finally, apply the “addEventListener()” method and attach the event named “keydown” on the input field to detect if the Enter key is pressed and notify it using the alert dialogue box:

enterKey.addEventListener("keydown", (e) =>{
 if(e.key == "Enter"){
  alert("Enter Key is Pressed")
}
})

 
Output


We have compiled different methods to press Enter key programmatically in JavaScript.

Conclusion

To press enter key programmatically in JavaScript, utilize the “document.addEventListener()” method for attaching a particular event and notifying the user if the enter key is pressed via an alert, the “document.querySelector()” method to display the status of the pressed enter key on the DOM or the “document.getElementById()” method for applying an enter key verification on an input field. This article demonstrated the methods to programmatically hit the Enter key in JavaScript.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.