Javascript - Need To Remove Li Element When Clicked
I need some assistance with an assignment I'm working on. Using JavaScript, I need to use the .target event property in my removeItem() function to remove li elements when they ar
Solution 1:
Here's the part of your code to change (see comments):
functionaddPostToList(post) {
var postList = document.querySelector("ul");
var li = document.createElement("li");
//Don't apply removeItem here. Just give the function name//The event will be passed to removeItem
li.onclick = removeItem;
postList.appendChild(li);
li.innerHTML = post.print();
}
functionremoveItem(e) {
e.target.parentElement.removeChild(e.target);
}
Here's a full JSBin: http://jsbin.com/mivonanoga/1/
Post a Comment for "Javascript - Need To Remove Li Element When Clicked"