Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LinkButton does not invoke on click()

Tags:

jquery

asp.net

Why doesn't this work?

    <script src="Scripts/jquery-1.3.2.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            $('.myButton').click();
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:LinkButton id="ttt" runat="server" PostBackUrl="~/Default.aspx" CssClass="myButton">Click</asp:LinkButton>
    </div>
    </form>
like image 904
Hitz Avatar asked Jun 03 '09 06:06

Hitz


2 Answers

Do you want to submit the form, or add a Click event? Your link button translates to

<a id="ttt" class="myButton" href="javascript:WebForm_DoPos[...]">Click</a>

, so it has no on-click javascript. Therefore, .click(); does nothing.
I haven't test it, but maybe this will work:

eval($('.myButton').attr('href'));
like image 170
Kobi Avatar answered Oct 05 '22 19:10

Kobi


trigger('click') fires jQuery's click event listener which .NET isn't hooked up to. You can just fire the javascript click event which will go to (or run in this case) what is in the href attribute:

 $('.myButton')[0].click();

or

 ($('.myButton').length ? $('.myButton') : $('<a/>'))[0].click();

If your not sure that the button is going to be present on the page.

Joe

like image 36
JoeS Avatar answered Oct 05 '22 21:10

JoeS