This is a great little function that I whipped up and use to capitalize every word in a string. I can’t tell you how many times I’ve had to use this. Hope you find it useful as I have!
1 2 3 4 5 6 7 8 9 |
<script> function wordToUpper(strSentence) { return strSentence.toLowerCase().replace(/\b[a-z]/g, convertToUpper); function convertToUpper() { return arguments[0].toUpperCase(); } } </script> |
Thanks for this helpful tidbit! I had a similar need and your function provided the answer. In my case, I had to get the first letter of each word in a string (for initials of a person), so this is what mine looks like:
Can you explain EXACTLY how to use the script please. Where we place it etc
Thanks
Craig
Hi!
I found the same method for capitalizing every word, but was looking for keeping the other letters lowercase. When I saw your code, I facepalmed, for how easy it is, and why I haven’t thought of it before. So, thanks for it! : ) Great site anyway!
Greetings from Hungary.
Tamás Márton