DOM Manipulation
createElement, append, remove, forms
Skills taught: js.createElement, js.append, js.forms, js.dom-manipulation
Creating and appending elements
// Create an element
const card = document.createElement('div');
card.className = 'card'; // set class
card.id = 'user-card'; // set id
card.textContent = 'Hello World'; // set text
// Set attributes
card.setAttribute('data-id', '42');
card.setAttribute('aria-label', 'User card');
// Inline styles
card.style.backgroundColor = '#1a1f2b';
card.style.padding = '16px';
// Append to the page
document.querySelector('#container').append(card);
// Modern: append accepts strings and elements, multiple at once
const list = document.querySelector('ul');
const item1 = document.createElement('li');
item1.textContent = 'Item 1';
const item2 = document.createElement('li');
item2.textContent = 'Item 2';
list.append(item1, item2); // both added in one call
Insertion positions
const parent = document.querySelector('#list');
const newItem = document.createElement('li');
newItem.textContent = 'New';
parent.append(newItem); // add to END of parent
parent.prepend(newItem); // add to START of parent
const existing = document.querySelector('#existing-item');
existing.before(newItem); // insert BEFORE this element
existing.after(newItem); // insert AFTER this element
existing.replaceWith(newItem); // replace this element
// Remove an element
existing.remove();
// Or: parent.removeChild(existing);
Building a list dynamically
const users = [
{ id: 1, name: 'Prashant', role: 'Dev' },
{ id: 2, name: 'Ana', role: 'Designer' },
{ id: 3, name: 'Ben', role: 'PM' },
];
const ul = document.querySelector('#user-list');
// DocumentFragment — batch appends, one reflow
const fragment = document.createDocumentFragment();
users.forEach(user => {
const li = document.createElement('li');
li.dataset.id = user.id;
li.textContent = `${user.name} — ${user.role}`;
fragment.append(li);
});
ul.append(fragment); // one DOM write instead of three
Handling forms
const form = document.querySelector('#login-form');
form.addEventListener('submit', (event) => {
event.preventDefault(); // stop page reload
// Method 1: read individual inputs by name
const email = form.elements['email'].value;
const password = form.elements['password'].value;
// Method 2: FormData — reads all fields automatically
const data = new FormData(form);
console.log(data.get('email')); // value of <input name="email">
console.log(data.get('password'));
// Convert to plain object
const obj = Object.fromEntries(data);
console.log(obj); // { email: '...', password: '...' }
// Clear the form
form.reset();
});
// Disable submit button while submitting
const submitBtn = document.querySelector('#submit');
submitBtn.disabled = true; // prevent double-submit
submitBtn.textContent = 'Submitting...';
Frequently Asked Questions
Is innerHTML safe for inserting content?
Only if the content is static and not from user input. Never set innerHTML to user-provided text (from an input, URL, or API) — malicious HTML can inject scripts (XSS). For user content, use textContent. For dynamic HTML you control, innerHTML is fine.
What is the difference between append() and appendChild()?
appendChild() is the classic method — it takes only one Node. append() is modern — it accepts multiple nodes AND plain strings, and converts strings to text nodes automatically. Prefer append() in new code.