/ Published in: JavaScript
Nick Baker
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
//this will force a capitalize of the first character of each word on input //this function is meant to be update dynamically as the user types //example: // <input .... onkeypress="capMe(this)" ..... > function capAll(obj){ var val = obj.value; var val = toArray(val); //old function var newVal = ''; var cap_it = true; for(var i = 0; i < val.length; i++){ if(cap_it){ newVal += val[i].toUpperCase(); cap_it = false; } else{ newVal += val[i]; if(val[i] == ' ') cap_it = true; } } obj.value = newVal; } //takes a string and converts it to an array of characters function toArray(string){ var len = string.length; var array = new Array(); for(var i=0; i < len; i++){ array[i] = string.charAt(i); } return array; }