Reputation: 919
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.
Upvotes: 0
Views: 1780
Reputation: 6180
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
});
Upvotes: 2
Reputation: 28837
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:
Upvotes: 0