I'm using contenteditable to allow users to edit information on the webpage, but some fields have a length requirement, and I want them to know when they've hit that requirement, so I change the color of the text that will be chopped off when they hit return to red.
The issue is that when the text is changed with JavaScript, the browser moves the cursor to the front of the string. Does anyone know of a way I can prevent this behavior? As far as I can tell this this is an issue in all browsers.
$(element).keypress(function (event) {
if ($(element).text().trim().length > maxChars) {
$(element).html($(element).text().trim().substr(0, maxChars) +
"<span class=red>" + $(element).text().trim().substr(maxChars) + "</span>");
}
});