I'm trying to make a loop that changes the class of an element everytime it is executed
HTML
<select class="form-control" id="FormControlSelect2" onchange="changeButtonColor2(this)">
<option value="Random1">Random</option>
</select>
JS
const all_buttons = document.getElementsByTagName('button');
function changeButtonColor2(buttonThingy) {
if (buttonThingy.value === 'Random1') {
buttonsRandom();
}
}
function buttonsRandom() {
for (let i=0; i < all_buttons.length; i++ ){
all_buttons[i].classList.remove(all_buttons[i].classList[1]);
}
var choices = ['btn btn-primary', 'btn btn-success', 'btn btn-danger',
'btn btn-warning','btn btn-secondary','btn btn-info', 'btn btn-dark',];
var randomNumber = Math.floor(Math.random() * 7);
var randomClass = choices[randomNumber];
for (let i = 0; i < randomClass.length; i += 1) {
all_buttons[i].classList.add(randomClass[i]);
}
}
JS Error Message
InvalidCharacterError: The string contains invalid characters. (= last line of the JS code)
Is it possible to use the output of the variable? If not, what is the solution? Please advise.