'Create multiple elements
JavaScript
var textBlock = document.createElement('div');
textBlock.setAttribute('class', 'finalBlock');
var addFinalBlock = document.getElementsByClassName('block')[0];
addFinalBlock.appendChild(textBlock);
textBlock.innerHTML = "Make this block a text block";
// -------
var textBlock2 = document.createElement('div');
textBlock2.setAttribute('class', 'finalBlock');
var addFinalBlock2 = document.getElementsByClassName('block')[0];
addFinalBlock2.appendChild(textBlock2);
textBlock2.innerHTML = "Make this block a text block2";
// -------
var textBlock3 = document.createElement('div');
textBlock3.setAttribute('class', 'finalBlock');
var addFinalBlock3 = document.getElementsByClassName('block')[0];
addFinalBlock3.appendChild(textBlock3);
textBlock3.innerHTML = "Make this block a text block3";
// -------
var textBlock4 = document.createElement('div');
textBlock4.setAttribute('class', 'finalBlock');
var addFinalBlock4 = document.getElementsByClassName('block')[0];
addFinalBlock4.appendChild(textBlock4);
textBlock4.innerHTML = "Make this block a text block4";
I want to create 4 elements with different text. The class needs to remain the same. I think I am doing this with to much code.
Maybe anyone has some information how to make this code look more beautiful and more efficient?
Best wishes and thank you!
Solution 1:[1]
I believe a better approach will be to only update the DOM once using fragment.
var docFrag = document.createDocumentFragment();
for(var i = 0; i < divs.length; i++) {
docFrag.appendChild(divs[i]); // Note that this does NOT go to the DOM
}
document.body.appendChild(docFrag); // Appends all divs at once
Solution 2:[2]
This code will work fine. If you want to add multiple elements on same HTML tag.
var element1= "<br/>"
var element2="<input type='text' />"
var element3= "<br/>"
var onLine = document.getElementById("onLine"); /* This is <ul id="onLine"></ul> */
var on = document.createElement("div");
on.innerHTML = element1+ element3+ element3
on.setAttribute("style", "background-color:rgba(0, 0,
0, 0.3);color:white;border-radius:40px;margin-
bottom:30px;padding: 10px;width: 230px;");
on.appendChild(document.createTextNode('Custom text'));
onLine.appendChild(on);
Solution 3:[3]
You can use the Array.prototype.map() method. It is similar to .forEach() but you return the result.
const textblocks = ["textblock 1", "textblock 2", "textblock 3", "textblock 4"].map(text => {
let div = document.createElement("div"); // p tag might be better suited for textblocks
div.classList.add("finalBlock");
div.textContent = text; // for text only, use textContent
return div;
});
document.body.append(...textblocks); // append all in one function call
Solution 4:[4]
You can create a for loop to create multiple-element
let elementsCount = 3;
for (let i = 0; i < elementsCount; i++) {
const element = document.createElement('div');
document.body.appendChild(element);
}
Solution 5:[5]
If there is parent child relationship, most efficient way to insert multiple elements is using text as template:
const el = document.createElement('div');
el.innerHTML = `
<div class="final-block">Block One</div>
<div class="final-block">Block Two</div>
<div class="final-block">Block Three</div>
<div class="final-block">Block Four</div>
`;
// Then insert wherever you want
document.querySelector('#app').appendChild(el);
This is faster than iteration and easier to understand.
If you need to generate those children from an array:
const el = document.createElement('ul');
el.innerHTML = ['One', 'Two', 'Three'].reduce((a, c) => a + `<li>${c}</li>`, '');
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Eyal Ofri |
| Solution 2 | |
| Solution 3 | User |
| Solution 4 | MD SHAYON |
| Solution 5 |
