I'm using an answer given here Count textarea characters but need to modify it to count down characters in a text field input AND change colour as it hits certain count down numerical limits, to give the user some form of warning they are approaching the field limitation.
This is what I have put together so far, on my own. It actually works! (Amazingly). First, this is what I started with:
$(window).load(function() {
$("#input_4_1").keyup(function() {
$("#count_4_1").text("Characters remaining: " + (2550 - $(this).val().length));
});
});
And this is what I have re-invented it as:
$(window).load(function() {
$("#input_4_1").keyup(function() {
if (2550 - $(this).val().length >= 501) {
$("#count_4_1").text("Characters remaining: " + (2550 - $(this).val().length));
} else if ((2550 - $(this).val().length <= 500) && (2550 - $(this).val().length >= 101)) {
$("#count_4_1").text("<span style=\"color: #55a500;\">Characters remaining: " + (2550 - $(this).val().length) + "</span>");
} else if (2550 - $(this).val().length <= 100) {
$("#count_4_1").text("<span style=\"color: #ff0000;\">Characters remaining: " + (2550 - $(this).val().length) + "</span>");
}
});
});
And this is what amazingly works. With one exception - and here comes the question. I know next to nothing about JS. When the character number read-out is between 2550 and 500 the display if fine. When the character read-out becomes 500 to 100 the read-out shows:
<span style="color: #55a500;">Characters remaining: 499</span>
So how do I get the JS to actually kick the css into proper display effect, instead of simply echoing out on the page??
If relevant, jQuery is in use.