0

I have been trying to get the value of an input of paste, but it always returns nothing:

$('form#post-game :input[name="header"]').keyup(function(){
    var str = $(this).val();
    IsImageValid(str, "#post-game-img-header");
});

$(document).on('paste','form#post-game :input[name="header"]',function(e) {
    var str = $('form#post-game :input[name="header"]').val();
    IsImageValid(str, "#post-game-img-header"); // there is a console log in this function
});

The keyup works fine, But the paste does not.

Tristan Cunningham
  • 909
  • 2
  • 10
  • 24

2 Answers2

2

Use the custom event afterpaste, paste method fires imediately some content is pasted, but some time s gives null value.

This afterpaste method fires after 0 ms of paste method, and works fine in every case.

//Custom event After paste
$('HTML').on('paste', function (e) {
    e = $.extend({}, e, { type: 'afterpaste' });
    window.setTimeout(function () { $(e.target).trigger(e); }, 0);
});

//changed paste to afterpaste
$(document).on('afterpaste','form#post-game :input[name="header"]',function(e) {
    var str = $('form#post-game :input[name="header"]').val();
    IsImageValid(str, "#post-game-img-header"); // there is a console log in this function
});
sudhansu63
  • 6,025
  • 4
  • 39
  • 52
0

Use .on('input propertychange', instead of .on('paste',.

Try this:

$(document).on('input propertychange','form#post-game input[name="header"]',function(e){ 
    var str = this.value;
    IsImageValid(str, "#post-game-img-header"); // there is a console log in this function
});

More info:

Sergio
  • 28,539
  • 11
  • 85
  • 132
  • Responds to ALL property changes including keypresses. Not very useful if you want to take action based soley upon a user pasting text. – Johann Jan 16 '14 at 11:55
  • @AndroidDev, its delegated to `form#post-game input[name="header"]`, so its just for that input, how would you do it? – Sergio Jan 17 '14 at 08:02