Accesskey in Jquery

Asked

Viewed 145 times

1

On one page I have several buttons with their respective accesskey (hot key for it), as I do for when this button is called by the key to execute the action click on it in Jquery. Example: I have the Send button, with shortcut by the A key, when I press the A key (accesskey of the same) I call the button click function in Jquery.

  • If you can, comment on the code you are using.

1 answer

1


I don’t know exactly how you are adding these event headphones. Adding this to the question makes your implementation clearer.

Some examples of how you can do this:

If you’re adding locally

if what you wear is something like: $('.button').on('click, keyup', function(){ then you can add a check of the event type like this:

$('.button').on('click, keyup', function(e){
    if (e.key == 13){
        // correr a lógica do código para a tecla Enter, e depois
        $(this).click();
    }
    // etc

or alternatively if (e.type != 'click') $(this).click();, but if you already have a logic to detect the key it implies that it is a keystroke event.

If you have a global handset

If you’ve got something like that $(document).on('keyup', function(){ which will check which key then you should have an object that stores the relationship between buttons and keys... for example:

Button 1

and then you can cache it at the beginning of page loading:

var btnsTeclas = {};
$('button[data-tecla]').each(function(){
    var tecla = $(this).data('tecla');
    btnsTeclas[tecla] = this;
});

and when you know what the key is, just do it:

var teclaPressionada = event.key; // ou outra lógica que tenhas para saber a tecla pressionada
$(btnsTeclas[teclaPressionada]).click();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.