Hers’s question which I need answer
Exercises: Level 1 Question 1
I tried to append the child dynamically and get the result vertical not in the compact manner as you will see in the question output when you go to the link
let hh = document.querySelector('h1') hh.style.textAlign = 'center' let hh1 = document.querySelector('h2') hh1.style.textAlign = 'center' let hh2 = document.querySelector('h3') hh2.style.textAlign = 'center' for (i = 0; i <= 100; i++) { let p1 = document.createElement('div'); { if (i % 2 == 0) { p1.className = 'Container' p1.style.fontSize = '25px' p1.style.backgroundColor = '#91E537' p1.textContent = i; p1.style.padding = '55px' p1.style.margin = '1px' p1.style.textAlign = 'center' p1.style.width = '20px' document.body.appendChild(p1); } else { p1.className = 'Container' p1.style.fontSize = '25px' p1.textContent = i; p1.style.backgroundColor = '#E5D037' p1.style.padding = '55px' p1.style.margin = '1px' p1.style.textAlign = 'center' p1.style.width = '20px' document.body.appendChild(p1); } } if (i >= 2) { let flag = 0; for (j = 2; j <= i / 2; j++) { if (i % j == 0) { flag = 1; break; } } if (flag == 0) { p1.className = 'Container' p1.style.fontSize = '25px' p1.style.backgroundColor = '#E55137' p1.textContent = i; p1.style.padding = '55px' p1.style.margin = '1px' p1.style.textAlign = 'center' p1.style.width = '20px' document.body.appendChild(p1); } } }
<h1>Number Generator</h1> <h2>30 days of JavaScript</h2> <h3>Author:ABC</h3> <div class="container"></div>
You can see the code of both HTML and javascript above!!! Do help with the code where I can easily append the data, and don’t use box elements I need simple code for this. I tried to do it with a few HTML styles but it didn’t help me, also using insert-adjacent text also didn’t work. Try to make changes only on javascript code, not HTML,if it’s possible else make minimum changes on HTML
I am using HTML5 AND CSS3
Advertisement
Answer
The idea here is to create a container for all of the blocks and set the display style attribute of this container to flex, style.flewrap to wrap and you can control how many blocks you want per line using style.width attribute.
After creating this element you would want to append to it your dynamically created blocks like p2.appendchild(p1);
Here is the code :
let p2 = document.createElement('div'); p2.className= 'p2'; p2.style.display = 'flex'; p2.style.flexwrap = 'wrap'; p2.style.width = '800px' for (i = 0; i <= 101; i++) { ... for every document.body.append(p1); --> p2.append(p1); ... } document.body.appendChild(p2);