Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    May 2009
    Posts
    59
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Simple jQuery keypress capitalization problem.

    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 :
    PHP Code:
    $(function () {
        $(
    "input:text:first").keypress(function () {
            $(
    this).val($(this).charAt(0).toUpperCase());
        });
    }); 
    the problem is it doesn't work, can you help me? :/

  • #2
    Senior Coder Spudhead's Avatar
    Join Date
    Jun 2002
    Location
    London, UK
    Posts
    1,856
    Thanks
    8
    Thanked 110 Times in 109 Posts
    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:

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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •