• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3815
  • Last Modified:

How to set text of a label control using JQuery

I am trying to set the text of a lable control using JQuery. The Label control is an Asp.net label control.
Can someone look at my two lines of JQuery below, that are not working. I am not sure if the JQuery syntact is correct.


$('#fooLabel').val('Hello World'); // first try Not working

$('fooLabel').val(''Hello World'); // Second tryn Not working




<asp:Label ID="fooLabel" visible="true" runat="server"></asp:Label>
0
brgdotnet
Asked:
brgdotnet
  • 3
1 Solution
 
GaryCommented:
runat="server" will change the ID of the control
If the jquery is in the page you can assign the resulting ID in the script
e.g.
$('#<%=fooLabel.ClientId%>').text('Hello World');

Open in new window

if the jquery is in an external file you will need to add a class to the label that you can reference

e.g.
<asp:Label ID="fooLabel" class="fooLabel" visible="true" runat="server"></asp:Label> 
$('.fooLabel').text(''Hello World');

Open in new window

0
 
GaryCommented:
Edited above to set the text not val
0
 
brgdotnetcontractorAuthor Commented:
Hi Gary,

Are you saying that if I remove the runat=server that it will fix the problem? Also, the JQuery is in the same page of the Aspx file. Not in an external file.
0
 
GaryCommented:
If it's in the same page then you can just use the first example
If you remove the runat="server" then this will work
$('#fooLabel').text('Hello World');

This is also assuming the jquery code is in a document ready function or at the end of the page.
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now