Sukhjeevan
Sukhjeevan

Reputation: 3156

How to fire a hyperlink trigger event using Jquery

Anybody suggest me how to fire hyperlink click event using jquery's trigger function?

ASPX:

<asp:LinkButton runat="server" ID="lnkClickMe" Text="Click Me" ></asp:LinkButton>    

JQUERY:

$("#lnkClickMe").trigger('click');

$("#lnkClickMe").click(function(){
   alert('clicked');
});

Upvotes: 0

Views: 1314

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038930

Your client side code is correct. The only caveat is that ASP.NET could mangle the id of the control and it might not be lnkClickMe at runtime. You could use a class selector or use the ClientID server side method to get the actual id:

$('#<%= lnkClickMe.ClientID %>').click(function(){
   alert('clicked');
});


$('#<%= lnkClickMe.ClientID %>').trigger('click');

In ASP.NET 4.0 you could configure predictable names by using the ClientIDMode setting:

<system.web>
    <pages clientIDMode="Predictable"></pages>
</system.web>

Upvotes: 3

Related Questions