logo

Creating New HTML Elements

March 22, 2021

The document.createElement() method creates an HTML element specified by an HTML tag name.

const pElem = document.createElement("p");
pElem.textContent = "Paragraph created using createElement method";
document.body.appendChild(pElem);

When you accessed the document object model (DOM) using code like above, you are using methods available on an instance of the Document class.

For each webpage loaded, an instance of Document is created, called document, which represents the entire page's structure, content, and other features.

When you are using string methods like below, you are using a method available on an instance of the String class.

const str = "The quick brown fox jumps over the lazy dog";
const wordsArr = str.split(" ");

Every time you create a string in your code, that string is automatically created as an instance of String, and it has several common methods and properties available on it.