-1

I am writing an animation and I want to add class to the divs that are being created each time in the for loop but every time

const canvas = document.getElementById("canvas");
 function divCreater() {
    for (let i = 0; i < 100; i++) {
        document.createElement("div");
        let div = document.querySelectorAll("div");
        div.addClass('div');
        div.width = "(Math.random()*10)px";
        div.height = "(Math.random()*10)px";
        canvas.appendChild("div");
    }
};
divCreater();

I get the error that addClass is not a function

yasminia
  • 3
  • 1

1 Answers1

3

It's div.classList.add('div'); instead. Also document.querySelector("div"); to just select one element. You are also creating a div that you're not using. Do this instead:

let div = document.createElement("div");
/*let div = document.querySelectorAll("div");*/
Kubwimana Adrien
  • 2,463
  • 2
  • 8
  • 11