Click here to Skip to main content
15,890,947 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
why my jquery is not working in my content page for button click ?????

     <%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="JqueryTest._Default" %>

       <asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
      
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">

    <h2>
        Welcome to ASP.NET!
    </h2>
    <asp:Button ID="button" runat="server" Text="Click Me" />
    <p>
        To learn more about ASP.NET visit <a href="http://www.asp.net" title="ASP.NET Website">www.asp.net</a>.
    </p>
    <p>
        You can also find <a href="http://go.microsoft.com/fwlink/?LinkID=152368&clcid=0x409">
            title="MSDN ASP.NET Docs">documentation on ASP.NET at MSDN</a>.
    </p>
    

  <script src="Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" type="text/javascript"></script>

<script type="text/javascript">
$.(document).ready(function (){
    
        $("button").click(function () {
            $("p").hide();
        });
    });
</script>  
Posted

1 solution

Your selector $("button") is wrong. If you have an HTML id attribute with the value "button", id="button", you need to use an id selector $("#button"):
http://api.jquery.com/id-selector/[^].

The selector you uses can be used only if you have only one element "button", no matter what attributes it has, which makes this selector not really robust and potentially error-prone:
http://api.jquery.com/element-selector/[^].

It's always better to rely on unique element position in the DOM hierarchy or unique attribute value (with "id" being the best because it requires uniqueness). Please see:
http://api.jquery.com/category/selectors/hierarchy-selectors/[^],
http://api.jquery.com/category/selectors/attribute-selectors/[^],
http://api.jquery.com/category/selectors/[^].

In ASP.NET code, to make sure the HTML id attribute gets required value, use System.Web.UI.Control.ClientID and make sure you use correct ClientIDMode:
http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientid%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.web.ui.clientidmode%28v=vs.110%29.aspx[^].

Also, make sure that your jQuery script is put in the <body> HTML element, not <head>.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900