MMK
MMK

Reputation: 109

Getting JavaScript Error in master page using ASP.NET WEB Form

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.

Upvotes: 1

Views: 184

Answers (2)

sujith karivelil
sujith karivelil

Reputation: 29036

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;

Upvotes: 0

Mairaj Ahmad
Mairaj Ahmad

Reputation: 14624

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);" />

Upvotes: 1

Related Questions