How to find asp.net button control using jQuery
 
Author:
Filed Under: ASP.Net
Published Date: Aug 26, 2011
Views: 5189
 

Abstract: Here Mudassar Ahmed Khan has answered the question "How to find asp.net button control using jQuery"

 


Question
How to find asp button with jQuery
My asp.net button: <asp:Button ID="btnButton" runat="server" CssClass="PayButton" Text="Button" />
My jQuery:
$('#<%=btnButton.ClientID %>').click(function () {
This is not works.


Answer
You need to make use of the id$ syntax of jQuery to find the ASP.Net button and use the jQuery live to bind the click event handler in the following way
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.5.2.min.js"></script>
    <script type="text/javascript">
        $("input[id$=btnButton]").live("click", function () {
            return confirm("Do you want to submit?");
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:Button ID="btnButton" runat="server" CssClass="PayButton" Text="Button" />
    </form>
</body>
</html>