Create and add elements
Build elements with createElement and add them with append. Use textContent for user text.
JavaScript · Create and add elementsBeginner
const list = document.querySelector('#todos');
function addTodo(text) {
const li = document.createElement('li');
li.textContent = text; // safe even if text contains <b>
li.className = 'todo';
list.append(li);
}
addTodo('Buy milk');
addTodo('<b>not bold</b>');
list.firstElementChild.remove(); // remove the first onecreateElementappenddom