0

please if someone would be so kind and help me, i would be very happy, clearing i'm a beginner so help if you can, please.

the html

<div>
    <h2>TO-DO LIST</h2>
    <input type="text" id="tasks"  class="List" placeholder="add">
    <input type="submit" id="subButton" onclick="action()">
    <!-- <button type="button" id='resetBut' onclick="deleteIl()">Reset</button>-->
    <div>

   <ul id="myUl" ></ul>

the js

function action() {

  let item = document.createElement('li');
  let inputValue = document.getElementById('tasks').value;
  var t = document.createTextNode(inputValue);
  item.appendChild(t);

  if (inputValue === '') {
    alert("you must write something!");
  } else {
    document.getElementById('myUl').appendChild(item);
  }
  document.getElementById('tasks').value = '';

  const tasksDelete = document.createElement("button");
  tasksDelete.classList.add("delete");
  tasksDelete.innerHTML = 'X';
  item.appendChild(tasksDelete);
  document.getElementsByClassName("delete").onclick = log();
  //document.getElementsByClassName("delete").addEventListener("click", log());

  function log() {
    console.log("name")
  }
  //tasksDelete.document.getElementById('myUl');
  //tasksDelete.removeChild();
}

i'm trying to put a delete button on the elements 'li' (this works), and trying to use the removeChild() but i'm getting 'Uncaught ReferenceError: removeChild is not defined', so to see if it's working i've change the removeChild() for the log function, and now the log function appears in the console when a press the subButton and not when i click on the delete button.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • The only call to `removeChild()` is commented out. Post the actual code, not the code with the problem commented. – Barmar May 16 '22 at 20:01
  • `onclick = log();` should be `onclick = log`. You're calling the function immediately, not when the button is clicked. – Barmar May 16 '22 at 20:02
  • `document.getElementsByClassName("delete")` should be `tasksDelete`. – Barmar May 16 '22 at 20:03
  • my dear Barmar, when i use the removeChild() out off comment i get a 'Uncaught ReferenceError: removeChild is not defined'. – Bruna Santos May 16 '22 at 20:04
  • I know that. You said so in the question. But you should post the code that gets the error, not code with the error commented out. – Barmar May 16 '22 at 20:05
  • So we can try to run your code and see the error, then fix it. – Barmar May 16 '22 at 20:08
  • Why are you trying to remove a child of the delete button? Don't you want `item.remove()` to remove the entire item? – Barmar May 16 '22 at 20:10

0 Answers0