Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set text to label with jQuery?

I want to set text to label with jQuery after clicking on button. I wrote code and it works, but after I set text in my label, label return his old state. Here is my code:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="DynamicWebApplication.WebForm2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

    <head runat="server">
        <title></title>

        <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
        <script type="text/javascript">
            function f() 
            {
                $('#<%=Label1.ClientID%>').html("hello");  
            }
        </script>
    </head>

    <body>
        <form id="form1" runat="server">
            <div>
                <asp:Label ID="Label1" runat="server"></asp:Label>
                <p></p>
                <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="f();"/>
            </div>
        </form>
    </body>

</html>
like image 811
Rougher Avatar asked Aug 08 '11 18:08

Rougher


1 Answers

If your button is causing a postback then the changes will be lost after the page has reloaded. Try this -

function f() 
        {
            $('#<%=Label1.ClientID%>').html("hello"); 
            return false;  
        }
like image 193
ipr101 Avatar answered Oct 06 '22 23:10

ipr101