Mona Coder Mona Coder - 1 year ago 33
Javascript Question

Issue on Counting Text Box Left Character length in APT.NET and jQuery

I have a multi-line textbox in ASP.Net application which looks like this:


and a span to display the result of left character as:

<span id="NeedDescriptionLimit"></span><span> Character left</span>

Now I have jQuery code which is working perfectly on this DEMO as it is like :

<script type="text/javascript">
$(document).ready(function () {
// Text Box limit count.
var origCnt = 8;
alert("This is a test");

$('#NeedDescriptionText').keyup(function (e) {
var currentlen = $('#NeedDescriptionText').val().length;
if (currentlen <= origCnt) {
$('#NeedDescriptionLimit').html(origCnt - currentlen);
} else {
$(this).val($(this).val().substring(0, origCnt));


but it is not functioning on ASP.NET app. I put the
in the code to test if jQuery is working which it was and I am getting the alert on opening the page but the character counting not working at all.

Can you please let me know why this is happening?


Answer Source

one option:

add ClientIDMode="static" to the textbox

or replace this:


with this

$("<%= NeedDescriptionText.ClientID %>")


The ClientID value is generated by concatenating the ID values of each parent naming container with the ID value of the control. In data-binding scenarios where multiple instances of a control are rendered, an incrementing value is inserted in front of the control's ID value. Each segment is separated by an underscore character (_).