Govind
Govind

Reputation: 544

Using Jquery not able to get correct out put

I did a small style changing app using Jquery in Asp.Net 3.5 .I am having IE7. When i click on button the paragraph color should change. Problem is ,the color is changing,it is not stable.Means just like blinking. Please find the code below

<head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript" src="scripts/jquery-1.4.1.min.js"></script>
    <script type="text/javascript">

        $(document).ready(function() {

            $("#Button1").click(function() {

                $("p").css("background-color", "Yellow")

            });

        });

    </script>
</head>


<body>
    <form id="form1" runat="server">
    <div>

        <asp:Button ID="Button1" runat="server" Text="Button" />
            <p>This Should be in  <br/>
               yellow</p>

           </div>
    </form>
</body>
</html>

Upvotes: 0

Views: 124

Answers (4)

abhijit
abhijit

Reputation: 1968

$("input[id$='Button1']").click(function() {
button click stuff here.
});

Upvotes: 0

spender
spender

Reputation: 120518

Change:

$("#Button1").click(function() {

to

$("#<%=Button1.ClientID%>").click(function() {

Upvotes: 0

Dave Long
Dave Long

Reputation: 794

try this :

        $("#<%=Button1.ClientID %>").click(function() {

            $("p").css("background-color", "Yellow")

        });

When the Button renders on the client it won't have id = "Button1"

Upvotes: 1

Daniel A. White
Daniel A. White

Reputation: 191048

Its because ASP.NET changes the ID attribute to be something it can resolve.

If you are using ASP.NET 4, set the ClientIDMode to AutoID.

If you are using anything else, change your selector to use "#<%= Button1.ClientID %>"

Also, return false to prevent the form from being postbacked.

Upvotes: 2

Related Questions