I am working on a character count based on this post.
I would like to call the function on window load but seem to be having issues, otherwise everything works.
Can anyone lend a hand on getting the count on load please.
heres my fiddle
function countChar(val){
var len = val.value.length;
if (len >= 500) {
val.value = val.value.substring(0, 500);
$('#stat span').text(0);
}else {
$('#stat span').text(500 - len);
}
}
$(function(){
var inputT = $('#descTextArea').val();
//countChar(inputT);//this is breaking the code
$('#descTextArea').keyup(function(){
countChar(this);
});
});
Try this instead
jsFiddle example
In your code you were passing a string (the value) into your function which is trying to get the length of an element. Instead, by calling
countChar($('#descTextArea').get(0));you’re just passing the element and allowing the function to find the length of the input as you intended.