8

Possible Duplicate:
Capitalize the first letter of string in JavaScript

How do you force the first letter of the first word in a field to uppercase?

Community
  • 1
  • 1
user406151
  • 395
  • 3
  • 9
  • 15

2 Answers2

10

Asked before: How do I make the first letter of a string uppercase in JavaScript?

The correct answer:

function capitalizeFirstLetter(string)
{
    return string.charAt(0).toUpperCase() + string.slice(1);
}

You can use it like this:

var myString = "hello world";
alert(capitalizeFirstLetter(myString));

And it would alert Hello world

Community
  • 1
  • 1
Karl Laurentius Roos
  • 4,360
  • 1
  • 33
  • 42
2

You don't need JavaScript, it's enough with the CSS :first-letter pseudo-element.

If you want do it via JavaScript, it has been asked before:

str.replace(/^\w/, function($0) { return $0.toUpperCase(); })
Community
  • 1
  • 1
corbacho
  • 8,762
  • 1
  • 26
  • 23