+ 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).
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>
+ 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!
+ 2
https://code.sololearn.com/WIHd8vG5D965/?ref=app This is the code.
+ 2
Abhay Thank you!
0
👍