Trigger click event of an anchor tag with JavaScript/jQuery
This post will discuss how to trigger the click event of an anchor tag in JavaScript and jQuery.
1. Using jQuery
The idea is to use jQuery’s .click() method to trigger the “click” JavaScript event on an element. This is demonstrated below:
JS
1 2 3 4 5 |
$(document).ready(function() { $('#submit').click(function() { $('#search_link')[0].click(); }) }); |
HTML
1 2 3 4 5 6 7 8 9 |
<!doctype html> <html lang="en"> <body> <a id="search_link" href="https://www.bing.com/">Click Me</a> <div> <button id="submit">Click Anchor</button> </div> </body> </html> |
Note that $('selector')[0]
is used, as $('selector')
returns a jQuery object.
Alternatively, if you want to simply navigate to the href location, you can get the href value and assign it to the window.location object. The URL would then be loaded as if location.assign()
had been called with the URL.
JS
1 2 3 4 5 |
$(document).ready(function() { $('#submit').click(function() { window.location = $('#search_link').attr('href'); }) }); |
HTML
1 2 3 4 5 6 7 8 9 |
<!doctype html> <html lang="en"> <body> <a id="search_link" href="https://www.bing.com/">Click Me</a> <div> <button id="submit">Click Anchor</button> </div> </body> </html> |
To open the URL in a new window or tab, you can use the Window interface’s open() method.
JS
1 2 3 4 5 6 |
$(document).ready(function() { $('#submit').click(function() { var url = $('#search_link').attr('href'); window.open(url); }) }); |
HTML
1 2 3 4 5 6 7 8 9 |
<!doctype html> <html lang="en"> <body> <a id="search_link" href="https://www.bing.com/">Click Me</a> <div> <button id="submit">Open in new tab</button> </div> </body> </html> |
2. Using JavaScript
With pure JavaScript, you can trigger the “click” JavaScript event on an element, as shown below:
JS
1 2 3 |
document.getElementById('submit').onclick = function() { document.getElementById('search_link').click(); } |
HTML
1 2 3 4 5 6 7 8 9 |
<!doctype html> <html lang="en"> <body> <a id="search_link" href="https://www.bing.com/">Click Me</a> <div> <button id="submit">Click Anchor</button> </div> </body> </html> |
Alternatively, to navigate to the href location, you can assign the href value to the location object. To open in a new window or tab, you can use the window.open()
method.
JS
1 2 3 4 |
document.getElementById('submit').onclick = function() { var url = document.getElementById('search_link').href; window.open(url); } |
HTML
1 2 3 4 5 6 7 8 9 |
<!doctype html> <html lang="en"> <body> <a id="search_link" href="https://www.bing.com/">Click Me</a> <div> <button id="submit">Open in new tab</button> </div> </body> </html> |
That’s all about triggering the click event of an anchor tag with JavaScript and Query.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)