0

Hello,

I am trying to incorporate Javascript with ASP. I want to display in the Label how many characters the client has remaining in the textbox. The Label is not changing at all - any ideas?

<script type="text/javascript">
    function CheckCharCount(textBox, maxLength) {
        var charCount = maxLength - texBox.value.length;
        document.getElementById("lblCharCount").innerText = ";
        if (charCount >= 0) {
            //within limit - display in black
            document.getElementById("lblCharCount").style.color = "black";
        }
        else {
            //negative chars - display in red
            document.getElementById("lblCharCount").style.color = "red";
        }
    }
</script>

<form runat="server">
<div>
    <asp:Label ID="lblCharCount" runat="server">500 Characters Remaining</asp:Label>
    <asp:TextBox ID="textBox" runat="server" Height="53px" TextMode="MultiLine" onkeyup="CheckCharCounter(this, 500)"/>
</div>
</form>

Thanks,

Brittany

2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by JorgeM
0

first thing you need to do is fix your errors related to references..

For example, line 2 the function should be called "CheckCharCounter", line 2, 'texBox', should be "textBox".

here is a working example without the asp.net controls--html equivalents...

<body>
<div>
    <span id="lblCharCount">Test</span>
    <input id="textBox" onkeyup="CheckCharCounter(this, 5)" />
</div>

<script type="text/javascript">
    function CheckCharCounter(textBox, maxLength) {
       var charCount = maxLength - textBox.value.length;
        if (charCount >= 0) {
            document.getElementById("lblCharCount").style.color = "black";
        }
        else {
            //negative chars - display in red
            document.getElementById("lblCharCount").style.color = "red";
        }
    }
</script>
</body>

Edited by JorgeM

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.