Skip to content

Instantly share code, notes, and snippets.

@remitbri
Last active August 29, 2015 14:10
Show Gist options
  • Save remitbri/9c5ad42dbb6e728096c6 to your computer and use it in GitHub Desktop.
Save remitbri/9c5ad42dbb6e728096c6 to your computer and use it in GitHub Desktop.
a11y click events, not just click, for keyboards too
module.exports = someViewFramework.extend({
element : ".js-thatElement",
events : {
"click" : "doStuff",
"keypress" : "doStuffKeyboard"
},
doStuff : function() {
// foo
// bar
},
doStuffKeyboard : function(event) {
var code = event.charCode || event.keyCode;
// 32 = space
// 13 = CR
if ((code === 32) || (code === 13)) {
this.doStuff()
}
}
})
// http://www.karlgroves.com/2014/11/24/ridiculously-easy-trick-for-keyboard-accessibility/
function a11yClick(event){
if (event.type === "click") {
return true;
}
if (event.type === "keypress") {
var code = event.charCode || event.keyCode;
if ((code === 32) || (code === 13)) {
return true;
}
return false;
}
}
$('button').on('click keypress', function(event) {
if (a11yClick(event) === true) {
// do stuff
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment