Skip to content

Instantly share code, notes, and snippets.

@remitbri
Last active Aug 29, 2015
Embed
What would you like to do?
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