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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With