admin管理员组文章数量:1432153
I am trying to add a child element to a parent element as follows:
li
to be added toul
- Either when the enter button is clicked or the enter key on the keyboard is pressed, a new
li
and delete button should get added toul
.
My code isn't working right. Can someone help me with this, please?
HTML:
<h1>Simple Add/Remove Task</h1>
<h2>To do List</h2>
<input type="text" placeholder="enter your tasks" id="userInput">
<button id="enter">Enter</button>
<ul>
<li class="todos">Wake up <button>Delete</button></li>
<li class="todos">Study<button>Delete</button></li>
</ul>
CSS:
.done {
text-decoration: line-through;
}
Javascript:
var listItems = document.querySelectorAll(".todos");
var input = document.getElementById("userInput");
var ul = document.querySelectorAll("ul");
var button = document.getElementById("enter");
button.addEventListener("click", function(){
if(input.value.length>0){
var li = document.createElement("li");
li.appendChild(document.createTextNode(input.value));
ul.appendChild(li);
input.value = "";
}
});
input.addEventListener("keypress", function(){
if(input.value.length > 0 && event.which === 13){
var li = document.createElement("li");
li.appendChild(document.createTextNode(input.value));
ul.appendChild(li);
input.value = "";
}
});
for (var i = 0; i<listItems.length; i++){
listItems[i].addEventListener("click", function(){
this.classList.toggle("done");
});
}
I am trying to add a child element to a parent element as follows:
li
to be added toul
- Either when the enter button is clicked or the enter key on the keyboard is pressed, a new
li
and delete button should get added toul
.
My code isn't working right. Can someone help me with this, please?
HTML:
<h1>Simple Add/Remove Task</h1>
<h2>To do List</h2>
<input type="text" placeholder="enter your tasks" id="userInput">
<button id="enter">Enter</button>
<ul>
<li class="todos">Wake up <button>Delete</button></li>
<li class="todos">Study<button>Delete</button></li>
</ul>
CSS:
.done {
text-decoration: line-through;
}
Javascript:
var listItems = document.querySelectorAll(".todos");
var input = document.getElementById("userInput");
var ul = document.querySelectorAll("ul");
var button = document.getElementById("enter");
button.addEventListener("click", function(){
if(input.value.length>0){
var li = document.createElement("li");
li.appendChild(document.createTextNode(input.value));
ul.appendChild(li);
input.value = "";
}
});
input.addEventListener("keypress", function(){
if(input.value.length > 0 && event.which === 13){
var li = document.createElement("li");
li.appendChild(document.createTextNode(input.value));
ul.appendChild(li);
input.value = "";
}
});
for (var i = 0; i<listItems.length; i++){
listItems[i].addEventListener("click", function(){
this.classList.toggle("done");
});
}
Share
Improve this question
edited Jul 14, 2020 at 5:45
Cat
4,2562 gold badges12 silver badges18 bronze badges
asked Jul 14, 2020 at 4:13
new2codingnew2coding
371 silver badge7 bronze badges
3 Answers
Reset to default 3querySelectorAll
returns NodeList. NodeList does not contain appendChild method. So either try to replace ul.appendChild
with ul[0].appendChild
or just refactor your code
This solution modifies your item-adding code to include a delete button (and refactors the code into a dedicated function.)
All delete buttons (marked with a "delete-btn" class) trigger list-item removal. (The click event bubbles up to the ul, where the listener is attached.)
See the in-code ments for further explanation.
// Identifies DOM elements
const
listItems = document.querySelectorAll(".todos"),
input = document.getElementById("userInput"),
ul = document.getElementById("the-list"), // (NOT `querySelectorAll`)
button = document.getElementById("enter");
// Conditionally calls addItem function when button is clicked
button.addEventListener("click", function() {
if (input.value.length > 0) {
addItem();
}
});
// Conditionally calls addItem function when ENTER is pressed
input.addEventListener("keypress", function() {
if (input.value.length > 0 && event.which === 13){
addItem();
}
});
// Adds an item to the list (when called by either listener)
function addItem(){
const
li = document.createElement("li"),
txt = document.createTextNode(input.value),
btn = document.createElement("button");
// Puts txt & btn inside li
li.appendChild(txt);
btn.textContent = "Delete";
btn.classList.add("delete-btn"); // For later reference
li.appendChild(btn);
ul.appendChild(li); // Puts li inside ul
input.value = ""; // Clears input
}
// Listens for clicks on the `ul` or its descendants, even if the
// clicked element gets dynamically added later
ul.addEventListener("click", function(event) {
// Listeners can automatically access triggering events
const clickedElement = event.target; // Events have a target property
if(clickedElement.classList.contains("delete-btn")){
// Finds ancestor elements and removes li from ul
const
li = clickedElement.closest("li"),
ul = li.closest("ul");
ul.removeChild(li); // (Or we could add "done" to li's classList)
}
});
.delete-btn{
margin-left: 1em;
}
/*
.done {
text-decoration: line-through;
}
*/
<h1>Simple Add/Remove Task</h1>
<h2>To do List</h2>
<input type="text" placeholder="enter your tasks" id="userInput">
<button id="enter">Enter</button>
<ul id="the-list">
<li class="todos">Wake up<button class="delete-btn">Delete</button></li>
<li class="todos">Study<button class="delete-btn">Delete</button></li>
</ul>
Change this,
var ul = document.querySelectorAll("ul");
to this,
var ul = document.querySelector("ul");
As you have only one ul
in your html.
本文标签: appendchildAdding child element to a parent element using javascriptStack Overflow
版权声明:本文标题:appendchild - Adding child element to a parent element using javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745576842a2664383.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论