javascript - How to iterate through <span>s with Arrow keys -
i have few spans:
<span>item 1</span> <span>item 2</span> <span>item 3</span>
the items next another. user press arrow keys (left & right) & move through spans (in case update current object , dispatch action).
edit: not have sample code, absolute beginner. not expect post working solution here, have general advice how conceptually approach in react/redux.
here plain javascript example:
function move(dir) { var selectables = array.from(document.queryselectorall('.selectable')); var selected = document.queryselector('.selected'); var = selectables.indexof(selected); = (i+selectables.length+dir)%selectables.length; selected.classlist.remove('selected'); selectables[i].classlist.add('selected'); } document.addeventlistener('keydown', function (e) { switch(e.which) { case 37: return move(-1); case 39: return move(1); } });
.selected { background-color: yellow }
<span class="selectable selected">item 1</span> <span class="selectable">item 2</span> <span class="selectable">item 3</span> <p>set focus on page , press left/right arrow keys
Comments
Post a Comment