3

I have the following jQuery code:

$input.on('keyup', keyUpListener);

$input.on('input', inputListener);

// IE <= 8 fallback for input event.
$input[0].onpropertychange = function() {
    if (window.event.propertyName === "value") {                
        inputListener(window.event);
    }
};

$input is a jQuery input[type='text'].

Right now keyUpListener and inputListener are both executed when I type into the input or when I copy and paste something (onpropertychange is not fired because it is an IE only event).

But how can I tell JS to not fire inputListener if keyUpListener is executing and vice-versa?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
tonix
  • 6,671
  • 13
  • 75
  • 136
  • why do you ned both? Input would be enough http://stackoverflow.com/questions/17384218/jquery-input-event – gurvinder372 Dec 07 '15 at 12:50
  • `input` fires on `keydown` when I type, i.e. when I press and hold down the key the event listener is already fired. I only want to execute the code on `keyup` using `keyUpListener` **when I type**. In the other cases (`copy/paste`, characters inserted from `accent` suggestions in OS X and other cases `keyup` doesn't handle) I want to use the `inputListener`. – tonix Dec 07 '15 at 13:24

1 Answers1

0

Attach the specific event to the textbox?

$("#input").on('input', function () {
}

For instance the above - this way you can have multiple listeners not executing on the same input

EDIT

You are able to bind the paste function to the textbox also..

$("#input").bind('paste', function() {
var pasted = true;
}); 

Thus you can then have the below IF statement:

if (pasted) {
$input.on('input', inputListener);
} else {
$input.on('keyup', keyUpListener);
}
Dan Tromp
  • 189
  • 12
  • Sorry, I didn't understand you. I want the listeners to be bound to the same element but I do have different events. – tonix Dec 07 '15 at 13:25
  • You will have to rearrange the above to sit in your function – Dan Tromp Dec 07 '15 at 13:37
  • Yes that works for `copy/paste` but what about `selecting an auto-completion option, Linux-style middle-click paste, drag-and-drop, and lots of other things`? `input` handles everything correctly. – tonix Dec 07 '15 at 13:46
  • Yeah there are endless possibilities on how to populate a textbox, unfortunately I cant offer any other suggestions. – Dan Tromp Dec 07 '15 at 13:48