Add elements in JavaScript with createElement() and appendChild()
To add a list item, create an element with document.createElement('li') and attach it to a list in the document with appendChild(). The Document Object Model (DOM) represents HTML as a tree that you can modify with JavaScript.
Create an element and attach its text to a list
Put <ul id="list"></ul> in your HTML and run this script after the list. createTextNode() creates a plain text node. Attach it to the li, then attach the item to the list. See DOM Standard: createElement() for the element creation rules.
const list = document.querySelector('#list');
const item = document.createElement('li');
const text = document.createTextNode("New item");
item.appendChild(text);
console.log(item.isConnected); // false
console.log(list.appendChild(item) === item); // true
Before the item is attached to the document, item.isConnected is false. list.appendChild(item) returns that same item, so the comparison logs true. If list is null, check that the selector matches the ID and that the script runs after the list is created.
Move an existing node with appendChild()
Appending a node that already has a parent moves it to the end of the same parent or into a new parent. With at least two items, the code below moves the first item to the end and leaves the item count unchanged.
const list = document.querySelector('#list');
const first = list.firstElementChild;
if (first && list.children.length > 1) {
list.appendChild(first);
}
If you need independent copies, create new elements or use cloneNode(true) to copy descendants as well. Avoid duplicate IDs and register listeners added with addEventListener() again on the copy. See DOM Standard: appendChild() for the rules on moving nodes and return values.
Complete example: Add, move, and reset list items
Save this complete example in UTF-8 as dom-add-item.html and open it in a browser. Clicking Add item or pressing Enter in the field triggers the same submit handler. preventDefault() keeps the page in place. An input that is empty after trimming adds nothing. See HTML Living Standard for when Enter submits a form.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Add and move list items</title>
</head>
<body>
<form id="item-form">
<label for="item">Item to add</label>
<input id="item" type="text">
<button id="add" type="submit">Add item</button>
</form>
<button id="move" type="button">Move first to end</button>
<button id="reset" type="button">Reset</button>
<ul id="list"></ul>
<p id="status" aria-live="polite"></p>
<script>
const list = document.querySelector('#list');
const input = document.querySelector('#item');
const status = document.querySelector('#status');
function addItem(label) {
const item = document.createElement('li');
const text = document.createTextNode(label);
item.appendChild(text);
list.appendChild(item);
}
function reset() {
list.replaceChildren();
addItem("First item");
addItem("Second item");
input.value = '';
status.textContent = "Initial list restored.";
}
document.querySelector('#item-form').addEventListener('submit', (event) => {
event.preventDefault();
const label = input.value.trim();
if (label === '') {
status.textContent = "Enter an item to add.";
return;
}
addItem(label);
status.textContent = `Added ${label}.`;
input.value = '';
});
document.querySelector('#move').addEventListener('click', () => {
if (list.children.length < 2) return;
list.appendChild(list.firstElementChild);
status.textContent = "Moved the first item to the end.";
});
document.querySelector('#reset').addEventListener('click', reset);
reset();
</script>
</body>
</html>
The initial list contains First item, then Second item. Type Third item and press Enter to add it at the end. Select Move first to end to get Second item, Third item, First item. Reset restores the initial order and clears the field. Enter <em>example</em> to see the characters displayed literally.
Choose how to insert plain text
For a new element that needs only plain text, you can set item.textContent = label. Setting it on an element with children replaces those children. To keep the other children, append a text node instead. Element.append() accepts multiple strings or nodes and returns undefined. appendChild() takes a node and returns the one it appends.




