JSFiddle - React, Tailwind, and code Playground
HTML
<button id="decre">
prev
</button>
<button id="incre">
next
</button>
JavaScript
var tab = ["one", "two", "three", "four", "five"];
var btnPlus = document.getElementById('incre');
var btnMinus = document.getElementById('decre');
var counter = 0;
function next(){
if(counter < tab.length){
counter++;
}
if(counter == tab.length){
counter = 0;
}
console.log(counter + ' ' + tab[counter]);
}
function prev(){
if(counter > 0){
counter--;
}
if(counter < 0){
counter = tab.length;
}
console.log(counter + ' ' + tab[counter]);
}
btnPlus.addEventListener('click', next);
btnMinus.addEventListener('click', prev);