0

when I paste some text into this input It registers inputString as 0 until something is actually typed in there. is there a way to get the inputSting to update on paste? Thanks

$('#test').on('paste',function(){
  var inputString = this.value;
     if(inputString.length == 0) {
       $('#text').html('nothing');
    }else{
       $('#text').html('something');
user2014429
  • 2,497
  • 10
  • 35
  • 49

2 Answers2

1
$('.myElements').each(function() {
   var elem = $(this);
   // Save current value
   elem.data('oldVal', elem.val());
   // Look for changes
   elem.bind("propertychange keyup input paste", function(event){
      // If value has changed
      if (elem.data('oldVal') != elem.val()) {
       // Updated stored value
       elem.data('oldVal', elem.val());
       // Do action
     }
   });
 });

This will catch any change to your input.

Or use setInterval:

setInterval(function() { inputValue = $('#input_id').val(); }, 100);
dezman
  • 18,087
  • 10
  • 53
  • 91
1

The paste event fires before the clipboard content has actually been pasted in. A simple and reliable solution is to use a timer:

$('#test').on('paste', function() {
    var input = this;

    window.setTimeout(function() {
        var inputString = input.value;
        // Do stuff with inputString here
    }, 0);
});
Tim Down
  • 318,141
  • 75
  • 454
  • 536