Using jQuery to programmatically click an <a> link

JqueryHyperlinkClickAnchor

Jquery Problem Overview


I know this question has been asked before, but after a search on the web I can't seem to find a straight forward answer.

the HTML

<a id=myAnchor href=index.php>

the jQuery (Both of these do not work)

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

or

$('#myAnchor').trigger('click');

What is the simplest and most efficient way to achieve this?

Jquery Solutions


Solution 1 - Jquery

Try this:

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

It works for me.

Solution 2 - Jquery

window.location = document.getElementById('myAnchor').href

Solution 3 - Jquery

Click just triggers the click event / events not the actually "goto-the-links-href" action.

You have to write your own handler and then your $('#myAnchor').trigger('click'); will work...

$("#myAnchor").click(function(event)
{
  var link = $(this);
  var target = link.attr("target");

  if($.trim(target).length > 0)
  {
    window.open(link.attr("href"), target);
  }
  else
  {
     window.location = link.attr("href");
  }

  event.preventDefault();
});

Solution 4 - Jquery

<a href="#" id="myAnchor">Click me</a>

<script type="text/javascript">
$(document).ready(function(){
    $('#myAnchor').click(function(){
       window.location.href = 'index.php';
    });
})
</script>

Solution 5 - Jquery

Add onclick="window.location = this.href" to your <a> element. After this modification it could be .click()ed with expected behaviour. To do so with every link on your page, you can add this:

<script type="text/javascript">
    $(function () {
        $("a").attr("onclick", "window.location = this.href");
    });
</script>

Solution 6 - Jquery

I tried few of the above solutions but they didn't worked for me. Here is a link to the page which worked for me automatically click a link

Above link has many solutions and here's the one which worked for me,

    <button onclick="fun();">Magic button</button>

    <!--The link you want to automatically click-->
    <a href='http://www.ercafe.com' id="myAnchor">My website</a>

Now within the <script> tags,

<script>

     function fun(){
          actuateLink(document.getElementById('myAnchor'));
     }

     function actuateLink(link)
     {
          var allowDefaultAction = true;
          
          if (link.click)
          {
              link.click();
              return;
          }
          else if (document.createEvent)
          {
              var e = document.createEvent('MouseEvents');
              e.initEvent(
                   'click'     // event type
                   ,true      // can bubble?
                   ,true      // cancelable?
              );
              allowDefaultAction = link.dispatchEvent(e);           
          }
             
          if (allowDefaultAction)       
          {
              var f = document.createElement('form');
              f.action = link.href;
              document.body.appendChild(f);
              f.submit();
          }
    }

</script>

Copy paste the above code and click on clicking the 'Magic button' button, you will be redirected to ErCafe.com.

Solution 7 - Jquery

I had similar issue. try this $('#myAnchor').get(0).click();this works for me

Solution 8 - Jquery

If you are using jQuery, you can do it with jQuery.trigger http://api.jquery.com/trigger/

Example

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title></title>
	<script type="text/javascript" src="https://code.jquery.com/jquery-2.2.3.js"></script>
</head>
<body>
	<a id="foo" onclick="action()"></a>
	<script type="text/javascript">
		function action(){
			window.location.replace("http://stackoverflow.com/q/9081426/5526354")
		}

		$("#foo").trigger("click");
	</script>
</body>
</html>

Solution 9 - Jquery

Try this for compatibility;

<script type="text/javascript">
		$(function() {
			setTimeout(function() {
				window.location.href = $('#myAnchor').attr("href");
				
			}, 1500);
		});
	</script>

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
QuestionStephen CroftView Question on Stackoverflow
Solution 1 - Jqueryuser3346133View Answer on Stackoverflow
Solution 2 - Jqueryuser1106925View Answer on Stackoverflow
Solution 3 - JquerybangView Answer on Stackoverflow
Solution 4 - JqueryakiView Answer on Stackoverflow
Solution 5 - Jquerypolkovnikov.phView Answer on Stackoverflow
Solution 6 - JquerySaumilView Answer on Stackoverflow
Solution 7 - Jqueryyohan.jayarathnaView Answer on Stackoverflow
Solution 8 - JqueryMikhailView Answer on Stackoverflow
Solution 9 - JqueryVenkatView Answer on Stackoverflow