c# - How do I get label value as a parameter into a javascript function? - Stack Overflow

Hopefully this one is not too hard to understand but I just want the label values to be inputs into a j

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>

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>

Share asked Jul 9, 2010 at 22:48 Greg McNultyGreg McNulty 1,4665 gold badges28 silver badges50 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 3

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.

var span = document.getElementById('ctl00_cpMainContent_LabelCL'),
    text = span.firstChild;

functionX(text.textContent);

Working Demo

or if defined in the Page use script tags to render out the client id for the span using

<%= LabelCL.ClientID %>

Try this:

<asp:Label ID="LabelCL" runat="server" Text="A single int"></asp:Label>
<button onclick="displayContent(<%= LabelCL.ClientID %>)">click me</button>
<script>
    function displayContent(obj) {
        alert(obj.innerText);
    }
</script>

You almost had it:

<asp:Label runat="server" Text="<script type='text/javascript'>document.write(functionX(7));</script>"/>

Use this piece of code

onclick="myFunction(labelName.innerText)"

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742262073a4411091.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信