JavaScript DOM Element Creation
This guide explains how to create, modify, and add HTML elements to a web page using JavaScript's DOM manipulation methods. You'll learn to use createElement()
, textContent
, appendChild()
, and other essential techniques.
Basic HTML Structure (for reference):
<!DOCTYPE html>
<html>
<head>
<title>DOM Manipulation</title>
</head>
<body>
<div id="container"></div>
<script src="script.js"></script>
</body>
</html>
JavaScript Examples:
1. Creating a Simple Element
// Create a new paragraph element
const paragraph = document.createElement('p');
// Add text content
paragraph.textContent = 'This is a new paragraph created with JavaScript.';
// Add to the document
document.body.appendChild(paragraph);
2. Creating an Element with Attributes
// Create an image element
const image = document.createElement('img');
// Set attributes
image.src = 'https://example.com/image.jpg';
image.alt = 'Example image';
image.width = 200;
image.height = 150;
// Add to a container element
document.getElementById('container').appendChild(image);
3. Creating an Element with Styles
// Create a div element
const div = document.createElement('div');
// Add text content
div.textContent = 'This is a styled div';
// Apply styles
div.style.backgroundColor = 'lightblue';
div.style.padding = '20px';
div.style.borderRadius = '8px';
div.style.margin = '10px';
// Add to document
document.body.appendChild(div);
4. Creating an Element with Classes
// Create a button element
const button = document.createElement('button');
// Add text content
button.textContent = 'Click Me';
// Add classes
button.classList.add('btn', 'btn-primary');
// Add event listener
button.addEventListener('click', function() {
alert('Button clicked!');
});
// Add to document
document.body.appendChild(button);
5. Creating Complex Nested Elements
// Create a list structure
const list = document.createElement('ul');
list.id = 'myList';
// Create list items
for (let i = 1; i <= 3; i++) {
const listItem = document.createElement('li');
listItem.textContent = `Item ${i}`;
list.appendChild(listItem);
}
// Add to document
document.body.appendChild(list);
6. Using insertBefore()
// Create a new element
const newElement = document.createElement('div');
newElement.textContent = 'I was inserted before the container';
// Get reference element
const container = document.getElementById('container');
// Insert new element before the container
document.body.insertBefore(newElement, container);
7. Using append() (modern approach)
// Create multiple elements at once
const container = document.getElementById('container');
// Append multiple elements
container.append(
createElementWithText('p', 'First paragraph'),
createElementWithText('p', 'Second paragraph'),
createElementWithText('p', 'Third paragraph')
);
// Helper function to create element with text
function createElementWithText(tag, text) {
const element = document.createElement(tag);
element.textContent = text;
return element;
}
8. Creating Elements with InnerHTML
// Create element using innerHTML
const div = document.createElement('div');
div.innerHTML = `
<h2>Title</h2>
<p>Paragraph with <strong>bold</strong> text.</p>
<ul>
<li>Item 1</li>
<li>Item 2</li>
</ul>
`;
// Add to document
document.body.appendChild(div);
These examples demonstrate the primary methods for creating and manipulating DOM elements with JavaScript. Remember that while innerHTML
can be convenient, using DOM manipulation methods like createElement()
is generally more secure and performant.
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Terms Disclaimer About Us Contact Us
Copyright 2023-2025 © All rights reserved.