Return to Snippet

Revision: 6338
at May 16, 2008 18:29 by nurvzy


Initial Code
//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;
}

Initial URL


Initial Description
Nick Baker

Initial Title
Capitalize Words as you type

Initial Tags


Initial Language
JavaScript