jQuery: how to trigger anchor link's click event

Jquery

Jquery Problem Overview


I have a anchor link like

<a id="myanchor" href="http://google.com" target="_blank">Google</a>

How to open href target in a new tab programatically?

Jquery Solutions


Solution 1 - Jquery

Try the following:

$("#myanchor")[0].click()

As simple as that.

Solution 2 - Jquery

There's a difference in invoking the click event (does not do the redirect), and navigating to the href location.

Navigate:

 window.location = $('#myanchor').attr('href');

Open in new tab or window:

 window.open($('#myanchor').attr('href'));

invoke click event (call the javascript):

 $('#myanchor').click();

Solution 3 - Jquery

Even though this post is caput, I think it's an excellent demonstration of some walls that one can run into with jQuery, i.e. thinking click() actually clicks on an element, rather than just sending a click event bubbling up through the DOM. Let's say you actually need to simulate a click event (i.e. for testing purposes, etc.) If that's the case, provided that you're using a modern browser you can just use HTMLElement.prototype.click (see here for method details as well as a link to the W3 spec). This should work on almost all browsers, especially if you're dealing with links, and you can fall back to window.open pretty easily if you need to:

var clickLink = function(linkEl) {
  if (HTMLElement.prototype.click) {
    // You'll want to create a new element so you don't alter the page element's
    // attributes, unless of course the target attr is already _blank
    // or you don't need to alter anything
    var linkElCopy = $.extend(true, Object.create(linkEl), linkEl);
    $(linkElCopy).attr('target', '_blank');
    linkElCopy.click();
  } else {
    // As Daniel Doezema had said
    window.open($(linkEl).attr('href'));
  }
};

Solution 4 - Jquery

 window.open($('#myanchor').attr('href'));

               $('#myanchor')[0].click();

Solution 5 - Jquery

It worked for me:

     window.location = $('#myanchor').attr('href');

Solution 6 - Jquery

You cannot open in a new tab programmatically, it's a browser functionality. You can open a link in an external window . Have a look here

Solution 7 - Jquery

$(":button").click(function () {                
                $("#anchor_google")[0].click();
            });
  1. First, find the button by type(using ":") if id is not given.
  2. Second,find the anchor tag by id or in some other tag like div and $("#anchor_google")[0] returns the DOM object.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVenkat PapanaView Question on Stackoverflow
Solution 1 - JqueryIgor G.View Answer on Stackoverflow
Solution 2 - JqueryGvSView Answer on Stackoverflow
Solution 3 - JqueryTravis KaufmanView Answer on Stackoverflow
Solution 4 - JqueryJobelleView Answer on Stackoverflow
Solution 5 - JqueryVinod PoormaView Answer on Stackoverflow
Solution 6 - JqueryAbdul KaderView Answer on Stackoverflow
Solution 7 - Jqueryarun vatsView Answer on Stackoverflow