Похожие чаты

I have this: const [mainDiv, minorDiv] = document.createElement('div'); I want to

create two divs at once, unfortunately does not work at all and returns an error that says:

document.createElement(...) is not iterable

Is there a better way of creating more than one divs as the same time and assigning them to a different variables?

10 ответов

13 просмотров

createElement creates only 1 element at once, just call it twice, one for each variable

Generation Z-Aloyce Daniel Автор вопроса
D H
createElement creates only 1 element at once, just...

How if I want to create hundreds of divs, is it possible to loop it?

Generation Z-Aloyce Daniel Автор вопроса
D H
In that case, just loop

May you please show me how to do so?

Generation Z Aloyce Daniel
May you please show me how to do so?

First you need to determine the amount of divs you need, you already know that?

Generation Z-Aloyce Daniel Автор вопроса
D H
First you need to determine the amount of divs you...

Let's take 50 as an example, let's make 50 divs

Generation Z Aloyce Daniel
Let's take 50 as an example, let's make 50 divs

const divs = []; for (let i = 0; i < 50; i++) divs[i] = document.createElement('div');

D H
const divs = []; for (let i = 0; i < 50; i++) d...

This is a verbose way of doing it, you could try also with more abstraction const divs = Array.from({ length: 50 }, () => document.createElement('div'));

Generation Z-Aloyce Daniel Автор вопроса
D H
This is a verbose way of doing it, you could try a...

It seems like this is the best hence few lines makes all divs

Похожие вопросы

Карта сайта