1

I have the following link

<a href="@Url.Action("GetContentPage", "Home")" id="getStarted">
    <img class="img-responsive " src="img/1.jpg" alt="">
 </a>

All I need to do is trigger this link(which will trigger the Controller method).

I tried this $("#getStarted").trigger("click") but it doesn't work.

How can I trigger this link?

Arianule
  • 8,811
  • 45
  • 116
  • 174
  • Possible duplicate of [How can I simulate an anchor click via jquery?](https://stackoverflow.com/questions/773639/how-can-i-simulate-an-anchor-click-via-jquery) – Michael Freidgeim May 25 '18 at 13:21

2 Answers2

2

trigger method doesn't change url of the current tab, it executes bound event handlers. You can either use the native DOM click method:

$("#getStarted").get(0).click();

Or set the href property of the location object:

location.href = $("#getStarted").prop('href');
Ram
  • 143,282
  • 16
  • 168
  • 197
1

What about this, the link will be triggered on DOM ready :

$(function(){
   $(document).on('click','#getStarted', function(){
       window.location = $(this).attr('href');   
   });
   $('#getStarted').trigger('click');
});
Norlihazmey Ghazali
  • 9,000
  • 1
  • 23
  • 40