I decided to open a new question because I was trying to capitalize the first letter of words with 4 or more letters and with the exception of a few key words and I have this working code for that: http://jsfiddle.net/Q2tFx/11/
$.fn.capitalize = function () {
var wordsToIgnore = ["to", "and", "the", "it", "or", "that", "this"],
minLength = 3;
function getWords(str) {
return str.match(/\S+\s*/g);
}
this.each(function () {
var words = getWords(this.value);
$.each(words, function (i, word) {
// only continue if word is not in ignore list
if (wordsToIgnore.indexOf($.trim(word).toLowerCase()) == -1 && $.trim(word).length > minLength) {
words[i] = words[i].charAt(0).toUpperCase() + words[i].slice(1);
}
});
this.value = words.join("");
});
};
$('.title').on('keyup', function () {
$(this).capitalize();
}).capitalize();
But I have a problem while running the function on "keyup". I can't edit something in the middle of the input without getting the cursor at the end of the input. And I also can't "select all".
I know there are solutions to get the caret position and take care of things like this, but I really don't know how to integrate them to my code.
Any idea on how could I do this?