Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

hadis's avatar
Level 1

remove element by id

I want to remove one of items using id this is my code:

function AddToDo() {
      let item = document.getElementById("add").value;

      const id = 'item-' + Math.random().toString().replace('.', '')
      const htm = `<li id='${id}'><div class="form-check"><label class="form-check-label"><input class="checkbox" type="checkbox"><p class="todo">' ${item} '</p><i class="input-helper"></i></label><a class="clear" href="" id="clear" onclick="remove('${id}')" >clear</a><a class="edit" href="" id="edit" >edit</a></div></li>`;

      document.getElementById("todos").innerHTML += htm
}
function remove(id) {
       let dele = document.removeChild(id);
      
}

but it's not working,what is the problem?

0 likes
7 replies
Nakov's avatar

@hadis what about this:

document.getElementById(id).remove();
hadis's avatar
Level 1

oh yeah thnk u but nothing happen,when I click clear button it just refresh page!

hadis's avatar
Level 1

now I am using button

<button class="clear" id="clear" onclick="remove(${id})" >clear</button>

and this error: ' todolist.html:1 Uncaught ReferenceError: item is not defined at HTMLButtonElement.onclick '

Please or to participate in this conversation.