简体   繁体   中英

How to change a label's contents using Javascript?

I have a label that holds a value and a text box into which I will enter a number. I have another label, which should dynamically give the difference between the two as I type the number in the text box. How do I do this using Javascript? I tried:

<script type="text/javascript">
function compute_diff(){
    var lbl1 = document.getElementById("<%=label1.ClientID%>").value;
    var txtbox = document.getElementById("<%=textbox1.ClientID%>").value;
    var lbl2value = lbl1 - txtbox
    document.getElementById("<%=label2.ClientID%>").innerText = lbl2value;
    return true;
    }
</script>

I am calling this function on the OnKeyUp event, but it is not firing it. What is the correct way of going about this? I am developing the site using ASP.Net.

The line

var lbl2value = lbl1 - txtbox

will not work. You will need to use a string diff algorithm such as the one at

http://ejohn.org/projects/javascript-diff-algorithm/

In addition, consider using jQuery to ensure that this works cross browser as the implementation of innerText can differ from browser to browser.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM