Để tạo một element thì sử dụng cú pháp: document.createElement('tagName').

Sử dụng hàm element.appendChild(childElement) để thêm childElement vào bên trong element.

Chẳng hạn ta muốn tạo một tag <div> chứa một tag <p> và cho vào tag <body> của văn bản HTML:

// Create div element
const divElement = document.createElement("div")
 
// Create a text node
const textContent = document.createTextNode("This is a simple text content")
 
divElement.appendChild(textContent)
 
// Get body element
const bodyElement = document.querySelector("body")
 
// Add into body element
bodyElement.appendChild(divElement)

Có thể sử dụng thuộc tính element.innerHTML = 'element string' để chỉnh sửa nội dung HTML bên trong một tag nào đó. Chẳng hạn ta muốn thay thế toàn bộ nội dung trang web bằng dòng “This web has been hacked” thì cần làm như sau:

const bodyElement = document.querySelector("body")
 
bodyElement.innerHTML = `<p> This web has been hacked </p>`