-6

How do you convert text that you type into proper case as you type?

Example: “The Quick Brown Fox Jumps Over The Lazy Dog”

Edit-01: I want to make sure that it works On-Type. I found different methods to make this work On-Blur, but not On-Type.

  • Possible duplicate of [How to capitalize first letter of each word, like a 2-word city?](https://stackoverflow.com/questions/4878756/how-to-capitalize-first-letter-of-each-word-like-a-2-word-city) – Albert Einstein Jan 22 '19 at 11:38
  • 1
    Possible duplicate of [Convert string to title case with JavaScript](https://stackoverflow.com/questions/196972/convert-string-to-title-case-with-javascript) – Nick Parsons Jan 22 '19 at 11:43

1 Answers1

2

Try this

function toTitleCase(s) {
  return s.replace(/\w\S*/g, function(t) {
    return t.charAt(0).toUpperCase() + t.substr(1).toLowerCase();
  });
}
<input onkeyup="this.value=toTitleCase(this.value)" />
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
SajithG
  • 130
  • 4