N-WS
N-WS

Reputation: 181

Simplify hotkeys.js function

Is there a way to simplify this code instead of having to repeat the same information?...

shortcut.add("b",function() {
    $(".show_hide").trigger('click');  
    },{
    'disable_in_input':true
});

shortcut.add("a",function() {
    $("#about").trigger('click');  
    },{
    'disable_in_input':true
});

shortcut.add("w",function() {
    $(".show_hide_header").trigger('click');  
    },{
    'disable_in_input':true
});

shortcut.add("r",function() {
    $(".show_hide_l").trigger('click'); 
       },{
    'disable_in_input':true 
});

shortcut.add("up",function() {
    $("#prev").trigger('click');   
       },{
    'disable_in_input':true
});

shortcut.add("down",function() {
    $("#next").trigger('click');   
       },{
    'disable_in_input':true
});

It is for keyboard shortcuts http://www.openjs.com/scripts/events/keyboard_shortcuts/

Upvotes: 0

Views: 139

Answers (1)

liuyl
liuyl

Reputation: 175

var selectors = {'b': '.show_hide', 'a': '#about', 'w': '.show_hide_handler' },
    i;

function shortcutListener(selector){
    return function(){
         $(selector).trigger('click');
    }
}

for ( i in selectors){
    shortcut.add(i, shortcutListener(selectors[i]), {'disable_in_input' : true});
}

Upvotes: 1

Related Questions