Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 758
Nathan Avatar asked Aug 14 '11 02:08

Nathan


1 Answers

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>
like image 126
Šime Vidas Avatar answered Oct 17 '22 10:10

Šime Vidas