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

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -