2

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.

2 Answers 2

1

Your choices array, which gets passed to classList.add, is:

var choices = ['btn btn-primary', 'btn btn-success', 'btn btn-danger',
  'btn btn-warning','btn btn-secondary','btn btn-info', 'btn btn-dark',];

But spaces are invalid characters in a class name. classList.add will see that as you attempting to add a single class which contains a space, rather than as an attempt to add two separate classes. Assign the string to the className of the element instead (this will overwrite any previous class names too):

function buttonsRandom() {
  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].className = randomClass[i];
  }
}

While you can add multiple classes with classList.add by passing additional arguments (on browsers newer than IE11), eg

all_buttons[i].classList.add(...randomClass[i].split(' '));

That's overkill for this situation (and requires you to separately clear the classes beforehand).

Sign up to request clarification or add additional context in comments.

1 Comment

When an answer solves your problem, you may consider marking it as Accepted (check the checkbox on the left) to indicate that the issue is resolved :)
1

The below worked at last :)

function buttonsRandom() {
  var choices = ['btn-primary', 'btn-success', 'btn-danger',
  'btn-warning','btn-secondary','btn-info', 'btn-dark',];
  var randomNumber = Math.floor(Math.random() * 7);
  var randomClass = choices[randomNumber];
  console.log(randomClass);
  for (let i=0; i < all_buttons.length; i++ ){
    all_buttons[i].classList.remove(all_buttons[i].classList[1]);
    all_buttons[i].classList.add(randomClass);
  }

thank you CertainPerformance

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.