Arianule
Arianule

Reputation: 9063

Best way to trigger link using jquery

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?

Upvotes: 1

Views: 516

Answers (2)

Norlihazmey Ghazali
Norlihazmey Ghazali

Reputation: 9060

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');
});

Upvotes: 1

Ram
Ram

Reputation: 144739

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');

Upvotes: 2

Related Questions