

The document.createElement Function in JavaScript
source link: https://masteringjs.io/tutorials/fundamentals/create-element
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

The document.createElement Function in JavaScript
The createElement()
function in JavaScript is used to programatically add elements to the DOM.
It has one required argument, the type of element to create, like 'div'
or 'img'
.
For example, the button below creates and appends a new <div>
element.
Below is the HTML and JavaScript for the above button.
<div id="append-elements"></div>
<button onclick="addElement()">Click to Add</button>
function addElement() {
const doc = document.createElement('div');
doc.innerHTML = 'Hello World';
const container = document.querySelector('#append-elements');
container.appendChild(doc);
}
Recursive createElement()
Once you create an element, you can use methods like appendChild()
to create and append more elements.
const parent = document.querySelector('#nested');
// Create one element...
const child = document.createElement('div');
child.innerText = 'I am the parent\'s child';
// Create another element
const grandchild = document.createElement('h1');
grandchild.innerText = 'I am the grandchild';
// Append 2nd element as a child of the 1st elemtn
parent.appendChild(child);
child.appendChild(grandchild);
Below is the output of the above JavaScript.
I am the grandchild
More Fundamentals Tutorials
Recommend
-
45
MySQL’s Document Store – a quick-guide to storing JSON documents in MySQL using JavaScript and Python (and even SQL!) October 24, 2019
-
37
toDoc.js JS library to create create a Word document with Text/HTML Methods : toDoc.createSection(sectionType, contentType, content, position, align, size, font) Creates a Header or a Footer Section in the...
-
23
Dempster-Shafer evidence theory has been widely used in various fields of applications, because of the flexibility and effectiveness in modeling uncertainties without prior information. However, the existing evidence t...
-
5
SubscribeSubscribe to Tharun ShivStay up to date! Get all the latest & greatest posts delivered straight to your inbox19 May 2020/...
-
10
[GopherJS] createElement and createTextNode DOM Example December 30, 2016...
-
9
JavaScript Object Availability and Order of Events The document editor can be programmatically accessed using the JavaScript object TXTextControl. When using ASP.NET MVC or Web Forms, this object exists directly after load...
-
9
If you have just started learning JavaScript, you might have heard of the DOM. But what is it exactly?In this article, I will explain what the DOM is and provide some JavaScript code examples. We will take a look at how to select ele...
-
4
The yaml document from hell — JavaScript edition
-
4
dom JavaScript createElement 创建元素 您将学习如何使用 JavaScript document.createElement()创建新的 HTML 元素并将其附加到 DOM 树
-
6
dom JavaScript innerHTML 与createElement 那个更好 当您向元素添加属性时,使用innerHTML会使代码更少,更简洁,但是,innerHTML会导致Web浏览器...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK