Skip to content

Instantly share code, notes, and snippets.

@nateplusplus
Created January 11, 2018 21:06
Show Gist options
  • Save nateplusplus/6dcbf630620b07766eb06ab81d7d3d03 to your computer and use it in GitHub Desktop.
Save nateplusplus/6dcbf630620b07766eb06ab81d7d3d03 to your computer and use it in GitHub Desktop.
Accessible jQuery events - it's always more than a simple "click"
$('#elementId').on('click keydown', function(e) {
// Respond to mouse click or enter
if (e.type == 'click' || (e.type == 'keydown' && e.keyCode == 13)) {
e.preventDefault();
e.stopPropagation();
// Do stuff...
}
// Respond to down arrow
if (e.type == 'keydown' && e.keyCode == 40) {
e.preventDefault();
e.stopPropagation();
// Do stuff...
}
// Respond to up arrow
if (e.type == 'keydown' && e.keyCode == 38) {
e.preventDefault();
e.stopPropagation();
// Do stuff...
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment