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>
LVL 2
brgdotnetcontractorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.