0

I am wondering why below code is not working. Could anybody of you help me, please?

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="Server">
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#button").click(function () {
                $("#hide").toggle();
            });
        });
    </script>
    <asp:Button ID="button" runat="server" Text="button" OnClientClick="return false" />
    <div id="hide">
    <asp:Button ID="show" runat="server" Text="example" />
    </div>
</asp:Content>

EDIT: What in case when the button is in gridview or nested gridviev? When I use
$("#<%= button.ClientID %>") it returns me an error: "The name 'button' does not exist in the current context." Will you be able to help me in that case?

ironcurtain
  • 650
  • 9
  • 35

3 Answers3

4

Try:

$("#<%= button.ClientID %>").click(function () {
    $("#hide").toggle();
});
Khanh TO
  • 48,509
  • 13
  • 99
  • 115
3

Try this:

$("#<%= button.ClientID %>").click(function () {
     $("#hide").toggle();
});
Enam
  • 1,268
  • 1
  • 9
  • 16
1

This is answered elsewhere: How to stop ASP.NET from changing IDs in order to use jQuery

ASP.Net is not writing out the ID for the button as you would expect.

Community
  • 1
  • 1
Agile Jedi
  • 2,922
  • 2
  • 18
  • 17