/  Technology   /  JAVASCRIPT TRIGGER CLICK

JAVASCRIPT TRIGGER CLICK

 

The basic scenario we encounter while programming in javascript is that we are able to create any function based on the click of a button. When automating the testing of a web page or a site, we need to examine some additional functionality. Thus, the javascript technique of triggering a click event becomes more effective and reliable for dealing with the challenges described above.

The purpose of this tutorial is to introduce you to the trigger click event procedures in JavaScript.

How we can trigger click event in javascript:

In order to trigger the click event in JavaScript, we must use the following techniques:

a) click() method

b) addEventListener() and dispathEvent methods

As an example, we will use the methods outlined above to demonstrate the following:

Method 1:

Trigger click event using click event method in javascript:

The action is performed using the click method on the above-mentioned element. By using the user-defined function when the required button is clicked by the user, this method can be implemented by creating the button, getting its id, and triggering the click event.

Please refer to the following example for further clarification:

Example:

In the example below with the text “click here”, a button with an id and click event will be created;

<button type= "button" id= "btn" onclick= "clickEvent()">Click here</button>  

In javascript, we must specify its id in order to access the created button using the document.getElementById method. In order to perform the next operation, the click event will be invoked.

const get= document.getElementById('btn');  
get.click(); 

As a final step, we will print out the following function named “clickEvent()” using the click method on the console when the button is clicked.

function clickEvent() {  
console.log("now the Click Event is triggered ")  

Using the click method in an automated manner in the above output, we have observed that the button click here has been clicked.

Method 2:

Using the addEventListener() and dispatchEvent() methods, trigger the click event in javascript

The javascript event target interface provides this integral method.

This method registers an event listener. When the mentioned event occurs on the target, we will call the configured function.

Syntax of the event:

target.addEventListener( $type, $listener);  
target.addEventListener( $type, $listener, $options);  
target.addEventListener( $type, $listener, $useCapture);  

description of the syntax:

  • The above syntax contains a mandatory parameter called $type. Specifying the type of event to be monitored, the parameter accepts only one string. There is a case-sensitive nature to this parameter. Various events are supported by it, including keyboard, click, database, and input.
  • The $listener parameter is also a mandatory parameter. When an event of the mentioned type occurs, this parameter receives a notification as an object. An eventlistener interface or a javascript function should implement this object.
  • The $option parameter, however, is an optional parameter.

Example:

<button type="button" id="btn" onclick="openBingByMethod()">Open bing </button>  
const link = document.getElementById('btn');  
link.addEventListener('click', e => {});  
for(let i = 0; i < 5; i++) {  
link.dispatchEvent(new Event('click'));  

function openGoogleByMethod() {  
console.log("The required event is triggered")  

Output:

"The required event is triggered".
"The required event is triggered".
"The required event is triggered".
"The required event is triggered".
"The required event is triggered".

 

Leave a comment