tarun14110 tarun14110 - 5 months ago 9
jQuery Question

count char of TextArea (retrieved from database)

I am dynamically creating combination of

textarea
and
label
, each combination having different ids. e.g:
text-1
,
text-2
.... for
textarea
and
lab-1
,
lab-2
, .... for
label
s.

Each
label
displays the number of characters in each
textarea
. I am doing this by using following piece of code.

function updateCount(textAreaId, labelId) {
var cs = $('#'+textAreaId).val().length;
$('#'+ labelId').text(cs);
}


Calling above function
onkeyUp
and
onKeyDown
on
textarea
and its working fine.

In some of the
textareas
I am filling the data from the users last submission from database, In that case it shows default length as 0 till some key in not pressed in the
textarea
.

I want that as soon as page loads.

Answer

You can manually trigger keyup

$('textarea[id^="text-"]').keyup();

Make sure to place this line after binding the keyup events to all textareas