Hitz
Hitz

Reputation: 1071

Auto click a hyperlink

How do I automatically click a Hyerlink or LinkButton using jQuery

<asp:Hyperlink id="ttt" PostBackUrl="Hut.htm">Click</asp:Hyperlink>or<asp:LinkButton id="ttt" PostBackUrl="Hut.htm">Click</asp:LinkButton>

Upvotes: 0

Views: 7464

Answers (2)

jeroen.verhoest
jeroen.verhoest

Reputation: 5183

Calling the click event on a LinkButton (renders a html link) will not have much effect unless you have manually defined a javascript click event on it.

You need to execute the content in the href attribute (javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$phContent$ttt", "", true, "", "", false, true))):

eval($("a[id*='ttt']").attr("href"))

The code above will evaluate the javascript code in the href attribute and execute it.

Upvotes: 3

John Farrell
John Farrell

Reputation: 24754

You can use the jQuery click() function without any arguments to simulate a users click.

The only tricky part with asp.net controls is the ID will be some unwieldy generated ID like ctr_00_0001 so you'll have to use a class to target the button:

<asp:LinkButton id="ttt" PostBackUrl="Hut.htm" CssClass="myButton">Click</asp:LinkButton>

and the jQuery:

$('.myButton').click();

Upvotes: 3

Related Questions