Clicking an element trigger to another element in jQuery

This tutorial is going to be interesting. In this tutorial, we will click an element, but actually, the click will trigger to another element. So in this tutorial, we will learn how we can trigger clicking on an element to another element with a simple and proper example of code snippet using jQuery.

Let’s first write our HTML code:

<button id="clickWay">Clicking me will send you to Google</button>
<br><br>
<button id="googleLink" onclick="click_on_link();">Trigger click here</button>

In the above HTML code, we have created two buttons. Now we are going to do a thing when someone clicks the first button, the click will trigger the second button. It will be actually clicking an element by clicking another element.

Let’s see our jQuery JavaScript code:

$(document).on('click', '#clickWay', function() {
    $("#googleLink").click();
});

function click_on_link()
{
	alert("Clicked on second button done");
}

In the above jQuery JavaScript code, we set the click event listener on the first button and using the click method from jQuery, we trigger the click event to the second button.

To check if the click triggered to the second button successfully, we have also set the click event listener to this button and set an alert message.

Now if you test the complete code on your browser, you will notice that clicking the first button will show us the alert message that we set in the click event listener of the second button. That means, the clicking functionality of the second button done by clicking another button.

By getting the alert message can confirm that clicking the first button triggers the click of another button. That means we have successfully triggered clicking an element to another element. As we are showing the alert message by clicking the second button and clicking the first button showing the alert message, then sure, clicking an element triggers the click to another element.

I hope, you loved this tutorial.

2 responses to “Clicking an element trigger to another element in jQuery”

  1. Pfrimo says:

    Hey you saved my life Thanks a lot for sharing!

  2. hunter says:

    Thank you:) Great article. ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

Leave a Reply

Your email address will not be published. Required fields are marked *