Skip to content Skip to sidebar Skip to footer

Ctrl+V ( Paste ) Triggers JQuery's Keyup Function TWICE

How to make Ctrl+V or Paste not trigger the keyup function TWICE? This is a problem for me because I made an AutoComplete functionality, and it displays the same data twice when i

Solution 1:

Okay guys, Thank you all for your answers but as I go through some reading, a lot of blogs say this: "IF you're implementing an autocomplete functionality, DON'T rely on 'keyup' function"

So I changed my code to $('#this-id').bind('input', function() {});

And it worked, I don't have to worry now about pasting or anything else. I hope this helps to others too.


Solution 2:

you can try this

$(window).on('keyup', function (event) {
    if (!event.ctrlKey) {
        /* here your code for all keys besides CTRL ;-) */
    }
});

Solution 3:

You could use underscore's debounce to set delay for reading the keyup event.

See the working code at:

JSFiddle

JS:

var count = 0;
function lookup() {
    $('div#test').html($('input.text').val());
    count++;
    $('div#count').html(count);
}

$(document).keyup( _.debounce(lookup, 250, true) );

HTML:

<div>
    <input type="text">
</div>

Input: <div id="test"></div>
Keyup Count: <div id="count"></div>

underscore.js from:

http://documentcloud.github.com/underscore/underscore-min.js


Post a Comment for "Ctrl+V ( Paste ) Triggers JQuery's Keyup Function TWICE"