Greg McNulty Greg McNulty - 1 year ago 61
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


Answer Source

Try this

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

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

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


to retrieve the element.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download