Last active
August 29, 2015 14:10
-
-
Save remitbri/9c5ad42dbb6e728096c6 to your computer and use it in GitHub Desktop.
a11y click events, not just click, for keyboards too
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} | |
} | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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