Get text of label with jquery
Asked Answered
B

9

30

I want to do very simple thing, but I'm not success. I have button and label on my asp.net page and I want to get text of label after clicking on button. 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 type="text/javascript">
        function f() 
        {
            var g = $('<%=Label1.ClientID%>').val();  // Also I tried .text() and .html()
            alert(g);
        }
    </script>
</head>

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

Bendwise answered 7/8, 2011 at 12:6 Comment(2)
Can you give example how to use it?Bendwise
I don't see the jQuery script loaded anywhere in your code. Maybe that's the problem?Opportina
B
36

try this:

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

or this:

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

you are missing the #

add this in the head section:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
Backslide answered 7/8, 2011 at 12:10 Comment(1)
you need to add the jquery script included look at my edited answerBackslide
U
18

Try this

var g = $('#<%=Label1.ClientID%>').text();
Urbanity answered 7/8, 2011 at 12:14 Comment(0)
K
4

Try using the html() function.

$('#<%=Label1.ClientID%>').html();

You're also missing the # to make it an ID you're searching for. Without the #, it's looking for a tag type.

Khalil answered 7/8, 2011 at 12:9 Comment(1)
var g = $('<%=Label1.ClientID%>').val(); // Also I tried .text() and .html()Bendwise
A
2

No solution here worked for me. Instead I added a class to the label and was able to select it that way.

<asp:Label ID="Label1" CssClass="myLabel1Class" runat="server" Text="Label"></asp:Label>

$(".myLabel1Class").val()

And, as mentioned by others, make sure you have your jquery loaded.

Audacity answered 30/9, 2018 at 10:39 Comment(1)
I got this to work using $(".myLabel1Class").html() Thank you Dr Jones!Irtysh
V
1

Try:

<%=this.Label1.Text%>
Vinaigrette answered 28/4, 2015 at 11:4 Comment(0)
G
0

try document.getElementById('<%=Label1.ClientID%>').text or innerHTML OTHERWISE LOAD JQUERY SCRIPT AND put your code as it is....

Genevivegenevra answered 30/11, 2012 at 12:30 Comment(0)
C
0

for the line you wrote

var g = $('<%=Label1.ClientID%>').val(); // Also I tried .text() and .html()

you missed adding #. it should be like this

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

also I do not prefer using this method

that's because if you are calling a control in master or nested master page or if you are calling a control in page from master. Also controls in Repeater. regardless the MVC. this will cause problems.

you should ALWAYS call the ID of the control directly. like this

$('#ControlID')

this is simple and clear. but do not forget to set

ClientIDMode="Static"

in your controls to remain with same ID name after render. that's because ASP.net will modify the ID name in HTML rendered file in some contexts i.e. the page is for Master page the control name will be ConetentPlaceholderName_controlID

I hope it clears the question Good Luck

Christogram answered 15/1, 2019 at 9:6 Comment(0)
E
0

I suggest you to put ClientIDMode="Static", so you keep the same ID on the DOM

<asp:Label ID="Label1" runat="server" ClientIDMode="Static" Text="Label"></asp:Label>

Then on your js part: $("#Label1").text()

Electrocute answered 4/4, 2024 at 8:32 Comment(0)
C
-2

It's simple, set a specific value for that label (XXXXXXX for example) and run it, open html source of output (in browser) and look for XXXXXXX, you will see something like this <span id="mylabel">XXXXXX</span> it's what you want, the ID of <span> (I think it's usually same as Label name in asp code) now you can get its value by innerHTML or another method in JQuery

Carrie answered 7/8, 2011 at 12:15 Comment(4)
$('.mylabel').html(); for JQueryCarrie
That would select a class named mylabel, not an ID.Khalil
maybe this $('mylabel').html(); I'm not familiar with JQCarrie
$('#mylabel').html(), like the other posts.Khalil

© 2022 - 2025 — McMap. All rights reserved.