0

If I have an array of numbers split out like this:

var list = ['one', 'two', 'three', 'four, 'five', 'six', 'seven'];

And I have a div with class "four" and a pair of buttons:

<button id="buttonOne"></button>
<div class="four"></div>
<button id="buttonTwo"></button>

How can I make buttonOne reduce the class to "three" and buttonTwo increase it to "five"? I know this is probably kids stuff but any explanation would be very much appreciated.

Zubair Saif
  • 1,106
  • 1
  • 14
  • 29

3 Answers3

2

Get the index of the element's className in the array, add/subtract 1, then get the item at the index of the result:

var list = ['one', 'two', 'three', 'four', 'five', 'six', 'seven'];

const div = document.querySelector('div');
buttonOne.addEventListener('click', function(){
  var newClass = list[list.indexOf(div.className)-1]
  div.className = newClass;
  console.log(newClass);
})
buttonTwo.addEventListener('click', function(){
  var newClass = list[list.indexOf(div.className)+1]
  div.className = newClass;
  console.log(newClass);
})
<button id="buttonOne">reduce</button>
<div class="four"></div>
<button id="buttonTwo">increase</button>

If you want the class names to cycle (e.g, if the div has the seven class and clicking the 'increase' button causes the class name to become one), simply check whether the index is equal to 0 or the length of the array minus 1:

var list = ['one', 'two', 'three', 'four', 'five', 'six', 'seven'];

const div = document.querySelector('div');
buttonOne.addEventListener('click', function() {
  var index = list.indexOf(div.className);
  var newClass = list[index == 0 ? list.length-1 : index - 1]
  div.className = newClass;
  console.log(newClass);
})
buttonTwo.addEventListener('click', function() {
  var index = list.indexOf(div.className);
  var newClass = list[index == list.length - 1 ? 0  : index + 1]
  div.className = newClass;
  console.log(newClass);
})
<button id="buttonOne">reduce</button>
<div class="four"></div>
<button id="buttonTwo">increase</button>
Spectric
  • 30,714
  • 6
  • 20
  • 43
  • 1
    It would be better to use `this.nextElementSibling` and `this.previousElementSibling`. In a real application there won't be a single `div` element. – Barmar Aug 04 '21 at 20:29
0

Here handled some UX cases also, like disabling a button, when there is no action.

var list = ['one', 'two', 'three', 'four', 'five', 'six', 'seven'];
var listlength = list.length-1;
var ele = document.getElementsByClassName("four")[0];
var btnOne = document.getElementById("buttonOne");
var btnTwo = document.getElementById("buttonTwo");
btnOne.addEventListener("click", function(){
  var eleIndex = list.indexOf(ele.getAttribute("class"));
  var classname = list[--eleIndex];
      ele.setAttribute("class", classname);
      ele.innerHTML =classname;
      if(eleIndex == 0){this.disabled=true;return false};
      btnTwo.disabled = false;
});

btnTwo.addEventListener("click", function(){
  var eleIndex = list.indexOf(ele.getAttribute("class"));
  var classname = list[++eleIndex];
      ele.setAttribute("class", classname);
      ele.innerHTML = classname;
      if(eleIndex ==listlength){this.disabled=true;return false};
      btnOne.disabled = false;
});
<button id="buttonOne" style="float:left;margin-right:10px;">Prev</button>
<div class="four" style="float:left;">four</div>
<button id="buttonTwo" style="float:left;margin-left:10px;">Next</button>
Salamon J
  • 1
  • 3
0

I would do it like this

var list = ['one', 'two', 'three', 'four', 'five', 'six', 'seven'];

//  create variables for the DOM nodes you care about
const div = document.querySelector('div.four');
const buttonOne = document.getElementById('buttonOne');
const buttonTwo = document.getElementById('buttonTwo');

//  a function that returns the current class and its position in the array
const currentClass = () => {
    const className = div.className;
    const index = list.indexOf(className);
    return {className, index};
};

const setClass = (index) => {
    div.className = list[index];
};

buttonOne.addEventListener('click', ev => {
    setClass( currentClass()['index'] -1 );
});

buttonTwo.addEventListener('click', ev => {
    setClass( currentClass()['index'] +1 );
});
button::before {
  content: attr(id);
}

div::before {
  content: attr(class);
}
<button id="buttonOne"></button>
<div class="four"></div>
<button id="buttonTwo"></button>
code_monk
  • 9,451
  • 2
  • 42
  • 41