I'm learning some Javascript, which I'm incorporating into another project. I have some input fields with maxlength set at 20. They currently have a border set to them via CSS:
.textInput:focus {
border: 2px solid green;
}
I'd like for the border of the input fields to go red when the user hits the maxlength limit - this part I've achieved:
$("#newsTitle").keypress(function() {
if($(this).val().length == 20) {
document.getElementById('newsTitle').style.borderColor = "red";
}
});
However, if the user is to delete some text, or backspace, I'd like the border to go BACK to green. Could someone give me some pointers on how to do this?
If theres a more efficient way to achieve what I have so far, do tell me. I feel my approach is a bit bulky and that theres no need to give the element ID twice if its within an if statement, which applies to the element ID in question.
Cheers,
Jack