views:

121

answers:

3

I want to make everything I write in a textfield to be capital letters. As I write, not after losing focus.

How do I do this using jQuery?

+2  A: 
$('input[type=text]').keyup(function() {
    $(this).val($(this).val().toUpperCase());
});
Gazler
-1 Only changes after losing focus, not as you type. http://jsfiddle.net/XJKgs/
Greg
Valid point, should be combined with Brandon's solution to properly change it client side. Changed the code slightly to update on keypress.
Gazler
I removed my downvote because it now does what the OP asks. But if I were a user of a website that did this "keyup" change, **I'd hunt down the person responsible**. Seeing the letters change as you type is disorienting, and trying to go back to edit what you typed is frustrating because the cursor keeps moving to the end. http://jsfiddle.net/HPapz/1/
Greg
You are correct hence my comment above, I voted for the CSS solution.
Gazler
+15  A: 

I would use CSS for this.

Just add text-transform: uppercase to your styling, and then on the server side you can convert it to uppercase.

input {
  text-transform: uppercase;
}
Brandon
You can add a class, or this style, programatically with jQuery if you wish, but this is probably what you need.
Stefan Kendall
interesting approach
mcgrailm
+1, nice, i was also thinking all kinds of jquery / js solutions, but this is better
Michel
excellent, I had a background image for the input fields I wanted uppercased, and this is PERFECT! THANK YOU!
fgualda87
For some reason that does not work on IE6 :/ any ideas why??
fgualda87
@fgualda, I'm afraid I don't know, sorry. It should work. According to the CSS compatibility (http://msdn.microsoft.com/en-us/library/cc351024%28VS.85%29.aspx) it should be working. I'd suggest asking on doctype.com. They're more likely to be able to help with styling issues.
Brandon
A: 

You may need to use combination of these.

using the text-transform style only renders the type in upper-case, although the value may well be lower case.

The javascript will only change current text to upper once the field changes or focus is lost

you can use gazler's jquery or simply inline javascript

e.g. onblur='javascript:this.value=this.value.toUpperCase();'

This means that the text value will appear uppercase and actually be uppercased when the value changes / control loses focus.

HTH Sam

sambomartin
i can use onKeyUp
fgualda87