JavaScript can build new elements and add them to the page.
Create and add
const li = document.createElement('li');
li.textContent = 'New item';
document.querySelector('ul').append(li);
append(el)— add at the end of a parentprepend(el)— add at the startel.before(x)/el.after(x)— add next to an element
Remove
el.remove();
Insert HTML quickly
parent.insertAdjacentHTML('beforeend', '<li>Item</li>');
Tip
When building many elements, create them first and add them in one step to keep the page fast.
Example
Example — To-do list
HTML
<style>
li { margin: 4px 0; }
li button { margin-left: 8px; color: crimson; border: none; background: none; cursor: pointer; }
</style>
<input id="task" placeholder="New task">
<button id="add">Add</button>
<ul id="todos"></ul>
<script>
const list = document.getElementById('todos');
const input = document.getElementById('task');
document.getElementById('add').addEventListener('click', () => {
const text = input.value.trim();
if (!text) return;
const li = document.createElement('li');
li.textContent = text;
const del = document.createElement('button');
del.textContent = '✕';
del.addEventListener('click', () => li.remove());
li.append(del);
list.append(li);
input.value = '';
});
</script>