How can I make the first letter of a variable always capital?

Possible Duplicate:
Capitalize the first letter of string in JavaScript

I am making something called "The HTML Quiz". It's made with JavaScript. When the user first enters it, they have to enter their name. Once they click submit, the name they enter is saved into a variable. It doesn't look nice when it says:

Dear nathan,

You are about to take The HTML Quiz. Please.....

I think it looks better like this:

Dear Nathan,

You are about to take The HTML Quiz. Please.....

But if the user enters their name starting with a lowercase letter, it will stay that way. Is there anyway to make that first letter turn into an uppercase letter if it is lowercase with jQuery ?

For example, here is a simple name enter script: http://jsfiddle.net/RcLt6/

If you enter your name in all lowercase, of course, it will be all lowercase. How can I make the first character entered always be capitalized no matter what?


This will do it:

str = str[0].toUpperCase() + str.slice(1);

Live demo: http://jsfiddle.net/qNJVQ/


Btw, put the INPUT inside the LABEL - that way you won't have to use the for attribute to associate the label with the input field:

<label>
    Name: <input type="text">
</label>
链接地址: http://www.djcxy.com/p/17862.html

上一篇: 在JavaScript / Jquery中使用首字母大写

下一篇: 我怎样才能让变量的首字母总是大写?