- How to Trigger Button Click on Enter Key Press Using Javascript
- Your Text
- How to Trigger Click Event in Javascript
- click() method
- MouseEvent Constructor
- How to Trigger Click Event in JavaScript
- How to Trigger Click Event in JavaScript?
- Method 1: Trigger Click Event in JavaScript Using click() Method
- Method 2: Trigger Click Event in JavaScript Using “addEventListener()” and “dispatchEvent()” Methods
- Conclusion
- About the author
- Sharqa Hameed
How to Trigger Button Click on Enter Key Press Using Javascript
In this tutorial, you will learn how to trigger button click on enter key press using javascript. This is something where you need to put a little extra effort to accomplish. We have a bunch of keys in our keyboards but Enter key has its significance.
It is a very common convention to provide your users with the ability to submit any sort of form data as soon as they click on the button or press Enter key on their keyboard. Such a thing will happen implicitly if you are using submit button within the form element.
If you are not using the form element, then you might end up looking for alternate solutions. In this tutorial, we are going to cover one of those alternate solutions. As you know, whenever a user interacts with the keyboard, certain events are triggered and the keypress event is one of them.
We are going to using keypress event to detect keyboard interaction as well as make use of event object to detect which key is pressed by the user.
In the following example, we are going to type some text in the input field and when we press Enter key, we will take that text and display it on the screen. We will do it first by clicking a button and later we will trigger this button click after Enter keypress. Please have a look over the code example and steps given below.
- We have 4 elements in the HTML file ( div , input , button , and h1 ). The div element is just a wrapper for the rest of the elements.
- The inner text for the button element is “Get” and for the h1 element is “Your Text” .
- We have done some basic styling using CSS and added the link to our style.css stylesheet inside the head element.
- We have also included our javascript file script.js with a script tag at the bottom.
Your Text
- We have select 3 elements input , button , and h1 using document.querySelector() and stored them in inputEl , btnGet , and display variables respectively.
- We have attached the click event listener to the button element.
- In the click event handler function, we are simply getting value from the input element and setting it as the inner text of the h1 element.
- We have attached the keypress event listener to the input element. The first parameter of the event handler function is an event object which provides information about the event.
- In the keypress event handler function, we are using the key property of the event object and checking whether the key pressed by the user is Enter key. If yes, then we will execute the click() method of the button element to trigger click.
let inputEl = document.querySelector("input"); let btnGet = document.querySelector("button"); let display = document.querySelector("h1"); btnGet.addEventListener("click", () => < display.innerText = inputEl.value; >); inputEl.addEventListener("keypress", (e) => < if (e.key == "Enter") btnGet.click(); >);
How to Trigger Click Event in Javascript
In this tutorial, you will learn how to trigger click event in javascript. If you do a basic search on google to find tutorials about triggering click event or simulating click event in javascript, then you will know there are too many ways to achieve this. Click event is nothing more than an action which is triggered by a user using mouse click on the web page.
In this tutorial, I am going to cover 2 basic methods which you can use to trigger click event programmatically in javascript.
click() method
Most of the elements in the DOM support click() method. We can leverage this method to trigger click event on any element. When you call click() method on an element, a click event is dispatched and event listener in response will execute event handler function. We are going to cover the same technique. Please have a look over code example and steps given below.
- We created 2 elements, button and div and wrapped them with another div which will act as container.
- Child div element is given an id of box which will display a square on the screen with a background color of red.
- We have done basic styling using CSS.
- We got reference of button and box using querySelector() method and stored them in btn and box variable.
- We added click() event listener to the button , which in response will trigger click event of box .
- We added click() event listener to the box , which in response will change the background color of the box from red to green.
let btn = document.querySelector("button"); let box = document.querySelector("#box"); btn.addEventListener("click", (e) => < box.click(); >); box.addEventListener("click", (e) => < box.style.backgroundColor = "green"; >);
MouseEvent Constructor
Events which occur due to user interaction by a pointing device such as mouse are part of MouseEvent contructor. The most common mouse events are click, dblclick, mousedown, mouseup etc. MouseEvent constructor takes 2 parameters.
First parameters specifies event name which should be string. In our case, it will be “click”. The second parameter is optional and it can have bunch of properties which can help you in specifying where you want to click on window or screen in terms of position, which mouse button should be pressed etc. I suggest you to have a look over MDN docs to know more about MouseEvent constructor.
In this example, we want to trigger click event and make sure shift key is pressed when it happens. The HTML and CSS code is same as shown above in the example. The only difference is in javascritp code. Please have a look over code example and steps given below.
- We got reference of button and box using querySelector() method and stored them in btn and box variable.
- We added click() event listener to the button . In the event handler function, we are creating a new mouse event using MouseEvent constructor.
- From the above discussion, you know MouseEvent constructor takes 2 parameters, so we are providing it an event name click and an optional object where shiftKey property is set to true.
- We added click() event listener to the box . In the event handler function, we are checking if shiftKey property is true or not in the event object. If yes, then change the background color of the box from red to green, otherwise do nothing.
let btn = document.querySelector("button"); let box = document.querySelector("#box"); btn.addEventListener("click", (e) => < var clickEvent = new MouseEvent("click", < shiftKey: true >); box.dispatchEvent(clickEvent); >); box.addEventListener("click", (e) => < if (e.shiftKey) < box.style.backgroundColor = "green"; >>);
How to Trigger Click Event in JavaScript
While programming in JavaScript, we often encounter situations where we want to invoke any function upon the button click. For instance, in the case of automation testing, you need to test some added functionality of a web page or a site. In such scenarios, the technique of triggering click events in JavaScript becomes very handy and efficient for countering the stated challenges.
This write-up will guide you about the procedure to trigger click events in JavaScript.
How to Trigger Click Event in JavaScript?
To trigger click event in JavaScript, apply the following techniques:
Now, we will demonstrate the use of the above-mentioned methods one by one!
Method 1: Trigger Click Event in JavaScript Using click() Method
The “click()” method is used to perform a click on the specified element. This method can be implemented by creating a button, getting its id, and triggering the click event when the button is clicked using a user-defined function.
Go through the following example for demonstration.
Example
In the following example, a button will be added with having “Click Me” as its id and its associated onclick event, which will access the “clickEvent()” function:
In JavaScript code, access the created button by specifying its id in the document.getElementById() method. After that, the “click()” method will be invoked to perform the further operation:
Finally, define the function named “clickEvent()” in such a way that it will print out the following message on the console when the button is clicked using the “click()” method:
The output of the above implementation will result as follow:
In the above output, it is observed that the button “Click Me” is clicked in an automated way using the “click()” method. Meanwhile, the clickEvent() function is accessed, and the specified message is logged on the console.
Method 2: Trigger Click Event in JavaScript Using “addEventListener()” and “dispatchEvent()” Methods
The “addEventListener()” method attaches an event handler to a DOM object and the “dispatchEvent()” method dispatches the specified event to the object. These methods can be used in combination to create a new Event object and execute the click event with the help of the specified function.
In the above syntax, “event” refers to the specified event, and “function” is the function on which the event is to be applied.
Here, “event” refers to the selected event object that needs to be dispatched.
Overview the following example.
Example
The first step is to include a button with an id, an onclick event and a value as discussed in the previous method:
Then, fetch the button and add the event “click” using the “addEventListener()” method and specify “e” in its argument which refers to the event(click) object. Then, the “dispatchEvent()” method will dispatch the click event to an object named “Event” as demonstrated:
const get = document. getElementById ( ‘btn’ ) ;
get . addEventListener ( ‘click’ , e => { } ) ;
get . dispatchEvent ( new Event ( ‘click’ ) ) ;
Lastly, define the same clickEvent() function as stated in the previous method to display the corresponding message when the “click” event is triggered:
We have compiled different methods to trigger click events in JavaScript.
Conclusion
To trigger click events in JavaScript, the “click()” method can be applied to fetch the button and invoke the added function accordingly. Also, the “addEventListener()” and “dispatchEvent()” methods can be utilized to include a specified event and dispatch it to a newly created object. This article demonstrated the methods to trigger click events in JavaScript.
About the author
Sharqa Hameed
I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.