Getting input value with jQuery

0

I can not get the value typed in an input field with jQuery:

HTML:

<input type="text" id="vendaMediaMensal" name="vendaMediaMensal">

jQuery:

var vendaMediaMensal = $("#vendaMediaMensal").val();

$("#vendaMediaMensal").focusout( function(){
    alert(vendaMediaMensal);
});
    
asked by anonymous 28.05.2015 / 21:49

3 answers

8

The problem is that you are getting the value before it exists. Leave to get .val() only in focusout :

var vendaMediaMensal = $("#vendaMediaMensal");
vendaMediaMensal.focusout( function(){
    alert(vendaMediaMensal.val());
});
    
28.05.2015 / 21:55
2

Put the var within the function:

$("#vendaMediaMensal").focusout(function(){
        var vendaMediaMensal = $("#vendaMediaMensal").val();
        alert(vendaMediaMensal);
    });
    
28.05.2015 / 21:55
0

Just as improvement I recommend using the:

 $(this).val();

So you promote the reusability of this event.

Hugs

    
21.08.2015 / 16:43