Leandro Cusack
Leandro Cusack

Reputation: 369

Javascript uppercase not working

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

Upvotes: 2

Views: 5857

Answers (4)

Kirby L. Wallace
Kirby L. Wallace

Reputation: 11

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.

Upvotes: 1

Selvakumar Arumugam
Selvakumar Arumugam

Reputation: 79830

You could rewrite your function in a better way. See below,

$('#University').blur(function(){
    this.value = this.value.toUpperCase();
});

Upvotes: 2

Pointy
Pointy

Reputation: 413720

The .toUpperCase() function returns a new string; it doesn't modify the existing string.

Upvotes: 6

cliffs of insanity
cliffs of insanity

Reputation: 3694

Change to this:

uni = uni.toUpperCase();

Strings are immutable, so you need to assign the result to the variable, overwriting the old string.

Upvotes: 16

Related Questions