1

Java Script not working in Content page using ASP.NET C# and I am trying to count the character of multi-line text box below is character count code. Getting Below error.

Failed to load resource: the server responded with a status of 404 (Not Found) Uncaught ReferenceError: txtComments is not defined(…)

Below is my code JS and Design code.

    <script type="text/javascript">

       function characterCounter(controlId, countControlId, maxCharlimit) {
           if (controlId.value.length > maxCharlimit)
               controlId.value = controlId.value.substring(0, maxCharlimit);
           else countControlId.value = maxCharlimit - controlId.value.length;
       }


   </script>
    <fieldset style="width: 280px;">
        <legend>Counting Remaining Characters example</legend>
        <asp:TextBox ID="txtComments" Width="280px" Rows="4" Columns="12" runat="server"
            TextMode="MultiLine" onkeyup="characterCounter(txtComments, this.form.remCount, 150);"
            onkeydown="characterCounter(txtComments, this.form.remCount, 150);" /><input type="text"
                name="remCount" size="3" maxlength="3" value="150" readonly="readonly" />
        characters left
    </fieldset>

When I am trying to type some thing values is not changing.

2 Answers 2

1

You need to pass this instead of txtComments

<asp:TextBox ID="txtComments" Width="280px" Rows="4" Columns="12" runat="server"
            TextMode="MultiLine" onkeyup="characterCounter(this, this.form.remCount, 150);"
            onkeydown="characterCounter(this, this.form.remCount, 150);" />
Sign up to request clarification or add additional context in comments.

9 Comments

@the upvoter : he forgot to pass, please edit the answer
But you did not mention in your answer, please update your answer. @Leopard
@MMK what i didn't mention in asnwer ?
@Leopard : don't act like smart. be smart. Keep in mind Now only you edited the answer and replaced txtComments with this. One more point you have to add is that, the method is expecting the TextBox's ID and you are passing the whole TextBox.
@un-lucky if you see function OP is not accessing it by id rather directly using it.
|
0

I suggest you to use this instead for the textbox ID, because the id will changed with respect to the page name(as well as the master page name if any) since it is a server side control. Which means the calling method will looks like this :

onkeydown="characterCounter(this, this.form.remCount, 150);"

Additional note: Since you are accessing the value of the Textbox from the javascript method, you need not to pass the ID of the textbox instead of that you can pass it as TextBox. so the passing parameter would be this not this.Id. and have to say thanks to Leopard for pointing this;

4 Comments

I have five multiline textbox in a single page and I have to check for all and your means this.txtComments, i have to keep. @un-lucky
Each TextBoxes will have different ids, right? so the this.id will give you the corresponding id. and it is not ` this.txtComments` it would be this.id
Then above answer one more answer of only this means getting confuse. @un-lucky
When I will press then only changes below count value and if i will copy and past then not change why?. @un-lucky

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.