Broncko
Broncko

Reputation: 193

Prevent default jquery-ui tab behaviour when using keyboard navigation

I'm using jquery-ui tabs and jeditable to inline edit the tab title. But navigating with the cursors in the editable text leads jquery-ui to navigate to the tab next to it.

How can i prevent the jquery default behaviour (disable keyboad navigation in tabs).

Cheers, Broncko

Upvotes: 5

Views: 2611

Answers (4)

Bradley Mountford
Bradley Mountford

Reputation: 8273

Just had to do this myself. This is what worked for me:

$.widget("ui.tabs", $.ui.tabs, {
    _tabKeydown: function (event) {
        if (event.keyCode !== 38 && event.keyCode !== 40) {
            this._super(event);
        }
    }
});

You can substitute any combination of keys using event.keyCode and even make it customizable with something like:

$.widget("ui.tabs", $.ui.tabs, {
    options: {
        overrideKeyCodes: [],
    },
    _tabKeydown: function (event) {
        var isOverride = false;
        if (Object.prototype.toString.call(this.options.overrideKeyCodes) === '[object Array]') {
            for (i = 0; i < this.options.overrideKeyCodes.length; i++) {
                if (event.keyCode === this.options.overrideKeyCodes[i]) {
                    isOverride = true;
                    break;
                }
            }
        }

        if (!isOverride) {
            this._super(event);
        }
    }
});

$('#MyTabs').tabs({ overrideKeyCodes: [ 38, 40 ] });

Or even better you can add your own custom behaviors:

$.widget("ui.tabs", $.ui.tabs, {
    options: {
        overrideKeyCodes: {},
    },
    tabKeydown: function (event) {
        if (this.options.overrideKeyCodes.hasOwnProperty(event.keyCode)) {
            if (typeof this.options.overrideKeyCodes[event.keyCode] === 'function') {
                 this.options.overrideKeyCodes[event.keyCode](event, this._super(event));
            }
        }
        else {
            this._super(event);
        }
    }
});

$('#MyTabs').tabs({
    overrideKeyCodes: {
        40: function (event, callback) {
            console.log(event.keyCode);
        },
        38: function (event, callback) {
            console.log(event.keyCode);
            if (callback) {
                callback();
            }
        },
        32: null //just let the space happen
    }
});

Upvotes: 0

Dmitry Kudelko
Dmitry Kudelko

Reputation: 11

It's possible to unbind keydown events when tabs are initialized:

$('#tabs').tabs({
  create : function() {
    var data = $(this).data('tabs');

    data.tabs.add(data.panels).off('keydown');
  }
});

Upvotes: 1

Andrey Pokhilko
Andrey Pokhilko

Reputation: 2658

A better solution from here http://www.daveoncode.com/2013/09/18/how-to-disable-keyboard-navigation-in-jquery-ui-tabs/ :

jQuery('.foo').tabs({
activate: function(e, ui) {
    e.currentTarget.blur();
    }
});

Upvotes: 2

Broncko
Broncko

Reputation: 193

Solved it by:

$.widget( "ui.tabs", $.ui.tabs, {
    options: {
      keyboard: true
    },
    _tabKeydown: function(e) {
      if(this.options.keyboard) {
        this._super( '_tabKeydown' );
      } else {
        return false;
      }
    }
});

Upvotes: 3

Related Questions