Search code examples
javascripthtmlonkeydown

Specific Keymapping JS


I'm not super familiar with DOM events and such. Currently, what I'd like to do is click on the first object, which is a HTML link with the class name "test", when the user presses 1 on the keyboard and or keypad as well. I'd be using the keydown event since it seems more supported then keypress. How would I go as to doing this?


Solution

  • Using jquery for example:

    $(document).on('keydown', function(event) {
        if(event.keyCode == 49 // press key 1 on keyboard
            || event.keyCode == 97) // press key 1 on keypad
            location.href = $('a.test').attr('href');
    });
    

    jsFiddle