Search code examples
javascriptjqueryjquery-widgets

Prevent jQuery widget event


I'm using Twitter bootstrap "TimePicker" widget. This widget has behavior of increasing/decreasing time when ".keydown" event is fired.

I need to block this default behavior. How I could achieve that?

Currently I try to intercept and block events on html input element directly. I see that .keydown events are intercepted and prevented, but TimePicker .keydown event still fires:

    $('#timeInput').keydown(function (e) {
        var keyCode = e.keyCode || e.which,
            arrow = { up: 38, down: 40 };

        switch (keyCode) {
            case arrow.up:
                window.debug.info("Key Up prevented");
                event.preventDefault();
                break;
            case arrow.down:
                window.debug.info("Key Down prevented");
                event.preventDefault();
                break;
        }
    });

Can I override "TimePicker" control default behavior?


Solution

  • You can remove keydown behavior anytime doing:

    $('#timeInput').unbind('keydown');
    

    or right after the widget initialization:

    $('#timeInput').timepicker(options).unbind('keydown');