Capitalizing the First Letter in Each Word of a String
<html>
<head>
<script language="JavaScript" type="text/javascript">
function capWords(str){
var words = str.split(" ");
for (var i=0 ; i < words.length ; i++){
var testwd = words[i];
var firLet = testwd.substr(0,1);
var rest = testwd.substr(1, testwd.length -1)
words[i] = firLet.toUpperCase() + rest
}
document.write( words.join(" "));
}
</script>
</head>
<body>
<script language="JavaScript" type="text/javascript">
capWords("This is a test.");
</script>
</body>
</html>
Related examples in the same category