JavaScript: How to get the value of a sender input element from a keyup event?

I need to capture the keyup event in order to provide a direct check when the user enters input (the change event fires only when the input loses focus).

I'm having trouble getting a modified input value that evnt activated.

The code also works on a timer to prevent multiple calls when a user enters (only works every 500 ms).

I have several inputs with the class "priceinput" and attach to the keyup event of each of them as follows:

<script language="javascript" type="text/javascript">
    var timer;
    $(document).ready(function() 
    {
        $(".priceinput").each(function() 
        {
            $(this).keyup(function(e) 
            { 
                clearTimeout(timer);
                timer = setTimeout(function() 
                {     
                //how to get the value of the input element that was changed?  
                var value = ???;
                    $.getJSON('/Validator/IsValidInput', { input: value },
                    function(response) 
                    {
                      //indicate if input is correct
                    });
                }, 500);
            });
        });
     });
</script>

To get the input value of the sender, I tried $(this).val, this.val(), e.target.val()but no one works.

How to get sender input value?

+5
2

, - "this" . - :

$('.priceinput').keyup(function(e) {
  var $input = $(this);
  clearTimeout(timer);
  timer = setTimeout(function() { 
    var value = $input.val();
    $.getJSON( ... );
  }, 500);
});
+6

Internet Explorer event.srcElement, Firefox event.target.
event.toElement event.relatedTarget.

(, , .value .val())

+1

All Articles