+ 2

Removing items from a to do list.

Hey, everyone! I've managed to do a very simple to do list. I've created a function to add elements in ordered list. Easy, right? Well, I have trouble figuring out how to remove those elements afterwards. I've tried implementing an remove child node method but failed miserably. Can anyone suggest a remove items from list function (or something similar)? I'm trying to do this with plain JavaScript (no jQuery or Angular js).

24th Jun 2020, 9:30 PM
Vasilis Karapas
Vasilis Karapas - avatar
5 odpowiedzi
+ 2
<button>Add Item</button> <button>Remove Item</button> <br> <ol></ol> <script> const btn = document.getElementsByTagName("button")[0]; const remove=document.getElementsByTagName("button")[1]; var olist; btn.onclick= ()=>{ const tx = document.querySelector(".txt").value; olist = document.querySelector("ol"); let items = document.createTextNode(tx); const li = document.createElement("li"); li.appendChild(items); olist.appendChild(li); }; remove.onclick=()=>{ if(olist.hasChildNodes()){ olist.removeChild(olist.lastChild); } }; </script>
24th Jun 2020, 9:51 PM
Abhay
Abhay - avatar
+ 3
Wow, ok. I figured it might need a conditional, but I wasn't sure how to check the condition properly if it did. Cool!
24th Jun 2020, 10:07 PM
Vasilis Karapas
Vasilis Karapas - avatar
24th Jun 2020, 9:30 PM
Vasilis Karapas
Vasilis Karapas - avatar
+ 2
Abhay Thank you!
24th Jun 2020, 10:11 PM
Vasilis Karapas
Vasilis Karapas - avatar
0
👍
24th Jun 2020, 10:12 PM
Abhay
Abhay - avatar