After a very effective and helpful response to my question here: javascript text field counter display I not have one additional question. The code (shown below) works wonderfully, however it ONLY displays after a keypress. How do I tweak it so that it not only updates on keypress, but also displays after the page has loaded so the user can see the character count before they focus on the field and type? Usually (but not always) there will be text already inside the text field saved from a previous session, so they counter needs to pick up from there.
Code in use currently:
$(window).load(function() {
$("#input_4_1").keyup(function() {
var diff = (2550 - $(this).val().length);
if (diff >= 501) {
$("#count_4_1").html("<span style=\"color: #55a500;\">Characters remaining: " + diff + "</span>");
} else if ((diff <= 500) && (diff >= 101)) {
$("#count_4_1").html("<span style=\"color: #ff6600;\">Characters remaining: " + diff + "</span>");
} else if (diff <= 100) {
$("#count_4_1").html("<span style=\"color: #ff0000;\">Characters remaining: " + diff + "</span>");
}
});
});
Thanks!