0

Is there a way how to handle only one of these events if both of them fired? If I change value and press key I end up rebinding grid twice because events arrive in sequentially. I understand that what I am getting is correct behaviour and I am getting what I asked for, but I need a way how to stop the second event from propagation. I am looking for solution at the event level (not isGridBinded flag).

$(op.$txtBox).on('change keypress', function (e) {
        if ((e.keyCode == 13 && e.type == 'keypress')  || e.type == 'change') {
            localWidget._rebindGrid();
        }
    });
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Vojtiik
  • 2,558
  • 1
  • 17
  • 21
  • http://stackoverflow.com/questions/2363360/javascript-fires-two-events-onkeypress-and-onclick similar issue - no answer. – Vojtiik Jul 01 '13 at 11:33
  • Could you provide a jsfiddle with relevant code?! – A. Wolff Jul 01 '13 at 11:38
  • @BrokenHeartღ your link is relative to delegation, here OP doesn't delegate events – A. Wolff Jul 01 '13 at 11:44
  • you have a mistake in your fiddle the if statement should also check for keyup if you handling keyup. if you change the keyup back to keypress, run it and then type in value, loose focus from the field then type in other value and press enter you will get two logs. – Vojtiik Jul 01 '13 at 11:45
  • @roasted check again this link is for .on as OP is asking. – Code Lღver Jul 01 '13 at 11:46
  • @Bobby_D_ correct, sorry for the mistake! – A. Wolff Jul 01 '13 at 11:46
  • @BrokenHeartღ Here OP is using .on() syntax relative/equivalent to bind, not to delegate/live. Again, this is not OP's issue here. – A. Wolff Jul 01 '13 at 11:47

2 Answers2

1

Use keyup event instead.

Otherwise, the only way i can think is to throttle it using a timeout:

DEMO

(function () {
    var timeout;
    $('input').on('change keypress', function (e) {
        clearTimeout(timeout);
        timeout = setTimeout(function () {
            if ((e.keyCode == 13 && e.type == 'keypress') || e.type == 'change') {
                console.log(1);
            }
        }, 0);
    });
})();
A. Wolff
  • 74,033
  • 9
  • 94
  • 155
  • Thanks a lot the keyup did the trick, the timeout solution would get me around the issue as well. Interesting that these two events behaves differently, I will read up on it a little – Vojtiik Jul 01 '13 at 12:30
  • Further more here is the best solution : http://stackoverflow.com/questions/8608145/jquery-on-method-with-multiple-event-handlers-to-one-selector - http://api.jquery.com/on/#on-events-selector-data – Vojtiik Jul 01 '13 at 13:03
0

Use the events like that:

$(op.$txtBox).on({
    change: function(){
          localWidget._rebindGrid();
     },
     keypress: function(e){
         if(e.keyCode == 13)
            localWidget._rebindGrid();
     },
});

Reference: JQuery .on() method with multiple event handlers to one selector

OR you can use the bind() function of jQUery.

Community
  • 1
  • 1
Code Lღver
  • 15,573
  • 16
  • 56
  • 75