Skip to content

Instantly share code, notes, and snippets.

@knorthfield
Created August 7, 2018 13:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save knorthfield/67362258a888047506cfb023d12c13bb to your computer and use it in GitHub Desktop.
Save knorthfield/67362258a888047506cfb023d12c13bb to your computer and use it in GitHub Desktop.
Proof of concept for keyboard nav of search results
<style>
a {
display: block;
}
:focus {
background-color: red;
outline: 0;
}
</style>
<input type="text" id="input" tabindex="0">
<div>
<a href="#1" tabindex="0">
<p><strong>Title</strong></p>
<p>Description</p>
</a>
<a href="#2" tabindex="0">
<p><strong>Title</strong></p>
<p>Description</p>
</a>
<a href="#3" tabindex="0">
<p><strong>Title</strong></p>
<p>Description</p>
</a>
<a href="#4" tabindex="0">
<p><strong>Title</strong></p>
<p>Description</p>
</a>
</div>
<script>
/* bling.js */
window.$ = document.querySelectorAll.bind(document);
Node.prototype.on = window.on = function (name, fn) {
this.addEventListener(name, fn);
}
NodeList.prototype.__proto__ = Array.prototype;
NodeList.prototype.on = NodeList.prototype.addEventListener = function (name, fn) {
this.forEach(function (elem, i) {
elem.on(name, fn);
});
}
$('input').on('keydown', function(event){
if (event.keyCode == 40) {
$('div')[0].firstElementChild.focus();
}
});
$('a').on('keydown', function(event){
switch (event.keyCode) {
case 38:
var prev = event.target.previousElementSibling;
prev ? prev.focus() : null;
break;
case 40:
var next = event.target.nextElementSibling;
next ? next.focus() : null;
break;
}
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment