I want to set a variable in a class name

let box = document.querySelector("#app > div:nth-child(`${i}`) > div:nth-child(5)");

let numbers = 80;

for(var i = 50; i < numbers; i++) {
   box.innerText = "1.5";
}

How can I keep the number here constantly increasing? I tried $ {} failed

( div:nth-child(`${i}`) )

I want to take the class of the boxes and change the text in the box one by one

16 thoughts on “I want to set a variable in a class name”

  1. You should wrap the whole string in “ and do it inside the for loop.

    const numbers = 80;
    
    for(let i = 50; i < numbers; i++) {
       let box = document.querySelector(`#app > div > div:nth-child(3) > div:nth-child(${i}) > div:nth-child(5)`);
       if(box) {
          box.innerText = "1.5";
       }
    }
    Reply

Leave a Comment