How do I refer to a label in javascript?

G

Guest

I have a javascript function that is called when I click my server-side
submit button. I want to validate that one of my labels is not empty before
allowing a submit.
I cannot figure out how to access the value stored in myLabel.text.

For hidden form variables I use the following syntax in javascript:
myForm.myHidden.value

I tried a bunch of different things with no luck! Thanks in advance for any
help!
 
G

Grant Merwitz

you would need to make the textbox an input with a runat=server tag.
then the javascript would be able to access the form value (regarding you've
named the form as well)

an ofcourse for .net to use the html input, you'de need that runat=server
tag as well as declaring the input in the code behind
 
J

Josh

I think serverside labels get rendered as <span> tags. View the source of
you page to check.

Then use

document.getElementByID

google it for more details
 
D

DotNet Coder

Hi jacobryce,

If the label is an <asp:label> control, then you can reference it in your
javascript code as:

<code>
<script language="javascript">
var labelText = '<% = Label1.ClientID %>';
alert(labelText);
</script>
</code>

HTH,
~d
 
R

Riki

jacobryce said:
I have a javascript function that is called when I click my
server-side submit button. I want to validate that one of my labels
is not empty before allowing a submit.
I cannot figure out how to access the value stored in myLabel.text.

For hidden form variables I use the following syntax in javascript:
myForm.myHidden.value

I tried a bunch of different things with no luck! Thanks in advance
for any help!

myForm.<%=myHidden.ClientID%>.value
 
G

Guest

I couldn't get that to work, but I did get the following code to work:
var labelText = "<%= myLabel.Text %>";
alert(labelText);

Thank you.
 
G

Guest

Thanks, I had already tried that with no luck. But maybe my syntax was
incorrect.
alert(document.getElementById("myLabel").innerText;
 
Joined
Jul 9, 2009
Messages
1
Reaction score
0
Get the label with JavaScript

Here is a way to get the label in a form with JavaScript :

shawnolson.net/a/1441/get-a-form-elements-label-with-javascript.html

(sorry it wouldn't let me make it a link since I haven't posted here enough times yet.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top