View Full Version : Simple jQuery keypress capitalization problem.

12-08-2010, 03:05 PM
What I want to do :
- I want to change the first character of the value of input:text:first
- When I want to type on that text box, the first Character should be in uppercase.

What I come up :

$(function () {
$("input:text:first").keypress(function () {

the problem is it doesn't work, can you help me? :/

12-08-2010, 03:39 PM
You're calling charAt() on the element, not its value, and you'd be setting the value to be just the upper case initial char, not the full string with an initial uppercase letter. Calling replace() with a regex may be a more efficient solution:

$("input:text:first").keyup(function () {
$(this).val($(this).val().replace(/^([a-zA-Z])/,function(s){return s.toUpperCase()}));