Greg McNulty Greg McNulty - 3 months ago 12
Javascript Question

How do I get label value as a parameter into a javascript function?

Hopefully this one is not too hard to understand but I just want the label values to be inputs into a javascript function. Might be better to explain with code:

ASP.NET Label code:

<asp:Label ID="LabelCL" runat="server" Text="A single int filled in by DB"></asp:Label>


Page Source:

<span id="ctl00_cpMainContent_LabelCL">7</span>


What I would like to achieve but am not sure how to do:

<span id="ctl00_cpMainContent_LabelCL"> <script type="text/javascript">functionX(7)</script> </span>


So basically just wrap the output int in the following:

<script type="text/javascript">functionX( VALUE FROM LABEL TEXT)</script>


within the

<span></span>

Answer

Try this

<asp:Label ID="LabelCL" runat="server" />

<script type="text/javascript">
    var value = document.getElementById("<%= LabelCL.ClientID %>").innerHTML;
    functionX(value);
</script>

If it is called just after render you can simply use LabelCL.Text, if you need the value after and if it can be edited you can do as the exemple above.

With jQuery you can use

$("[id$=LabelCL]")

to retrieve the element.