I am trying to change text input value after user fill it, to uppercase string:
$('#University').blur(function(){
if ( $(this).attr("value") != '') {
var uni = $(this).val();
uni.toUpperCase();
alert(uni);
$(this).attr("value", uni);
};
});
<input type="text" name="Universidad" size="45" class="required" id="University">
If I write "leandro" into the field, the alert(uni)
throws: "leandro" and not "LEANDRO". Any idea?
I could not use CSS because I must send uppercase data through this form, and css only change the rendering and not the value
I am trying to change text input value after user fill it, to uppercase string:
$('#University').blur(function(){
if ( $(this).attr("value") != '') {
var uni = $(this).val();
uni.toUpperCase();
alert(uni);
$(this).attr("value", uni);
};
});
<input type="text" name="Universidad" size="45" class="required" id="University">
If I write "leandro" into the field, the alert(uni)
throws: "leandro" and not "LEANDRO". Any idea?
I could not use CSS because I must send uppercase data through this form, and css only change the rendering and not the value
Share Improve this question asked May 21, 2012 at 19:42 Leandro CusackLeandro Cusack 3691 gold badge6 silver badges21 bronze badges4 Answers
Reset to default 16Change to this:
uni = uni.toUpperCase();
Strings are immutable, so you need to assign the result to the variable, overwriting the old string.
The .toUpperCase()
function returns a new string; it doesn't modify the existing string.
You could rewrite your function in a better way. See below,
$('#University').blur(function(){
this.value = this.value.toUpperCase();
});
toUpperCase()
is a method. It returns a value that you need to do something with.
You called the .toUpperCase()
method on uni
, but you didn't assign it anywhere.
uni = uni.toUpperCase();
... should help.