user998405
user998405

Reputation: 1339

How can i get the label value by using Jquery?

i am beginner of jquery.How can i get the label value through jquery? Below is my coding

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        this.Label1.Text = "asdasd";
    }
}

Html

<script src="../js/jquery-1.4.1.js" type="text/javascript"></script>
<script language ="javascript" type="text/javascript">
    $(document).ready(function () {

            var g = $('#<%=Label1.ClientID%>').text;
            alert (g);


        });

    });


 </script>

The alert function is not working :(

Upvotes: 0

Views: 9168

Answers (4)

Nazmul
Nazmul

Reputation: 7218

Please correct your script as follows and try

<script language ="javascript" type="text/javascript">
     $(document).ready(function () {
         var g = $('#<%=Label1.ClientID%>').text();
         alert (g);           
    });      

 //Comment out the last parenthesis
 //});    
</script> 

Upvotes: 1

Tyler Crompton
Tyler Crompton

Reputation: 12652

You have one too many });. Also you shouldn't have a space between alert and the left parentheses. Also text is a function so you should also add the () at the end of the statement.

Upvotes: 0

keyboardP
keyboardP

Reputation: 69362

You were close

var g = $('#<%=Label1.ClientID%>').text();

More information on text().

Upvotes: 0

Fyodor Soikin
Fyodor Soikin

Reputation: 80734

text is a function, not a property. Put a pair of parentheses after it:

    var g = $('#<%=Label1.ClientID%>').text();

Upvotes: 0

Related Questions