With jQuery, how do I capitalize the first letter of a text field while the user is still editing that field?

后端 未结 21 2439
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-27 16:26

I\'m looking for an example of how to capitalize the first letter of a string being entered into a text field. Normally, this is done on the entire field with a function, r

21条回答
  •  难免孤独
    2020-11-27 16:46

    Jquery or Javascipt doesn't provide a built-in method to achieve this.

    CSS test transform (text-transform:capitalize;) doesn't really capitalize the string's data but shows a capitalized rendering on the screen.

    If you are looking for a more legit way of achieving this in the data level using plain vanillaJS, use this solution =>

    var capitalizeString = function (word) {    
        word = word.toLowerCase();
        if (word.indexOf(" ") != -1) { // passed param contains 1 + words
            word = word.replace(/\s/g, "--");
            var result = $.camelCase("-" + word);
            return result.replace(/-/g, " ");
        } else {
        return $.camelCase("-" + word);
        }
    }
    

提交回复
热议问题