A short (~50 lines of code) tool that you can use to easily create DOM elements and their children. Similar to the original React way of creating components :]
with config.children
using just HTML template strings and a nested config object.
<body>
</body>
const articleTitle = "First Time Using createElement!";
const sectionOneTitle = "First Section";
const sectionOneCodeSnippet = "console.log('this really works!')";
const myArticle = createElement({
tag: "article",
attributes: {
id: "main-article",
},
classes: ["text-center", "container"], // as an Array
children: [
`<h1>${articleTitle}</h1>`, // an HTML string
{
// a createElement config object
tag: "section",
attributes: {
id: "section-1",
},
classes: "text-center container format-code", // as a string
children: [
`<h2>${sectionOneTitle}</h2>`,
`<code>${sectionOneCodeSnippet}</code>`,
],
},
],
});
document.body.appendChild(myArticle);
<body>
<article id="main-article" class="text-center container">
<h1>"First Time Using createElement!"</h1>
<section id="section-1" class="text-center container format-code">
<h2>First Section</h2>
<code>
console.log('this really works!')
</code>
</section>
</article>
</body>
with config.children
using all possible child formats
- HTML template string
- plain text string
- HTMLElement (DOM Node)
- Text (DOM Node)
- NodeList
- a nested
createElement
config object
<body>
<p>test</p>
<p>test</p>
<p>test</p>
<p>test</p>
<p>test</p>
<table id="table-id">
<thead>
<tr>
<th>title</th>
</tr>
</thead>
<tbody>
<tr>
<td>title content</td>
</tr>
</tbody>
</table>
</body>
const title = "My Container Title";
const sectionTitle = "My Section Title";
const sectionContent = "Section content...";
// note any node that is already attached to the document will be moved as children to the new element
const table = document.querySelector("#table-id");
const paragraphs = document.querySelectorAll("p");
// these elements are unattached (since they were just created)
const newDiv = document.createElement("div");
newDiv.textContent = "new div content";
const textNode = document.createTextNode("Text Node of plain text");
const config = {
tag: "div",
classes: ["text-center", "container", "grid"],
attributes: {
id: "main-grid",
customAttribute: "custom value",
},
children: [
`<h1>${title}</h1>`, // an HTML string
"some plain text", // plain text string
table, // an attached DOM element (HTMLElement)
newDiv, // an unattached DOM element (HTMLElement)
textNode, // an unattached DOM element (TextNode)
{
// a config object
tag: "section",
attributes: {
id: "section-1",
},
children: [
`<h2>${sectionTitle}</h2>`,
`<p>${sectionContent}</p>`,
paragraphs, // an attached list of DOM elements (NodeList)
],
},
],
};
const containerNode = createElement(config);
document.body.appendChild(containerNode);
<body>
<div
class="text-center container grid"
id="main-grid"
customattribute="custom value"
>
<h1>My Container Title</h1>
some plain text
<table id="table-id">
<thead>
<tr>
<th>title</th>
</tr>
</thead>
<tbody>
<tr>
<td>title content</td>
</tr>
</tbody>
</table>
<div>new div content</div>
Text Node of plain text
<section class="" id="section-1">
<h2>My Section Title</h2>
<p>Section content...</p>
<p>test</p>
<p>test</p>
<p>test</p>
<p>test</p>
<p>test</p>
</section>
</div>
</body>