I think i'm missing something or have a silly error. I'm trying to get the counter going for textarea & for some reason length of textarea value is always zero. Here is my same code for View:
@Html.TextAreaFor(x => x.Description, new { @maxlength = "4000", @onkeyup = "countChar(this)" })
@Html.Label("", " ", new { @id = "lblcount" })
my corresponding javascript is:
function countChar(val) {
var max = 4000;
var len = $("#txtDescription").val().length;
if (len >= max) {
$('#lblcount').text(' you have reached the limit');
$('#lblcount').attr("class", "lblCountRed");
} else {
var ch = max - len;
$('#lblcount').text(ch + ' characters left');
$('#lblcount').attr("class", "lblCountGreen");
}
};
The above code always sets label text to "4000 characters left" irrespective of number of characters I type inside textarea.