🤯 New: Coding Assessments Practice your skills on real-world programming challenges

Capitalize First Letter of Every Word on Keypress

Sam Deering

jQuery code snippet to make capitals of the first letter of every word in the string. This could be used to prevent users from entering all caps for titles or text when they input data into forms.

$(“input”).keyup(function() {

function toUpper(obj) {
var mystring = obj.value;
var sp = mystring.split(‘ ‘);
var wl=0;
var f ,r;
var word = new Array();
for (i = 0 ; i < sp.length ; i ++ ) { f = sp[i].substring(0,1).toUpperCase(); r = sp[i].substring(1).toLowerCase(); word[i] = f+r; } newstring = word.join(' '); obj.value = newstring; return true; } [/js]