I’m having an issue with the delete function. I’m at the point where I can remove only one line before pushing the submit button again. It seems that I can remove only the first paragraph that was submitted. It feels like eventListener applies only for the first line. Do I need a loop here?
Here is some of my code:
JavaScript
x
62
62
1
// Manipulation with DOM
2
3
const submitButton = document.getElementById('submit-btn');
4
const inputText = document.getElementById('input-text');
5
const showcaseUlList = document.getElementById('showcase-ul-list');
6
const showcaseLilist = document.getElementById('showcase-li-list');
7
8
// Submit event listener
9
10
submitButton.addEventListener('click', submitAddToDo);
11
12
// Submit function
13
14
function submitAddToDo(){
15
// Checking if the input is not empty
16
if(inputText.value === ''){
17
alert("You've must fill the input field");
18
return;
19
}
20
21
// Create div
22
const divToDo = document.createElement('div');
23
divToDo.classList.add('showcase-li-list');
24
25
// Create li
26
const liToDo = document.createElement('li');
27
28
// Create button(check)
29
const checkToDo = document.createElement('button');
30
checkToDo.innerHTML = '<i class="fas fa-check-circle id="check-checked"></i>';
31
32
// Create paragraph
33
const pToDo = document.createElement('p');
34
let inputValue = inputText.value;
35
pToDo.innerHTML = inputValue;
36
37
// Create i(trash)
38
const trashToDo = document.createElement('button');
39
trashToDo.classList.add('trash');
40
trashToDo.innerHTML = '<i class="fas fa-trash trash" id="trash-delete"></i>'
41
42
43
44
// All childs appends
45
showcaseUlList.appendChild(divToDo);
46
divToDo.appendChild(liToDo);
47
liToDo.appendChild(checkToDo);
48
liToDo.appendChild(pToDo);
49
liToDo.appendChild(trashToDo);
50
51
// Clear input field and delete show line
52
inputText.value = '';
53
showcaseLilist.remove();
54
55
// Remove task
56
const trashDelete = document.getElementById('trash-delete');
57
trashDelete.addEventListener('click', removeTask);
58
const removeParent = trashDelete.parentElement.parentElement.parentElement;
59
function removeTask(){
60
removeParent.remove();
61
}
62
}
JavaScript
1
35
35
1
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css" integrity="sha512-+4zCK9k+qNFUR5X+cKL9EIR+ZOhtIloNl9GIKS57V1MyNsYpYcUrUeQc9vNfzsWfV28IaLL3i96P9sdNyeRssA==" crossorigin="anonymous" />
2
<body>
3
<div class="container">
4
5
<!-- Header - welcome section -->
6
<header class="welcome-section">
7
<div class="welcome-text">
8
<h1>Welcome,</h1>
9
<p>Here you can make your own task manager</p>
10
<p class="text-xs">feel free to try it out!</p>
11
</div>
12
<img src="../IMG/todo-header.png" alt="" class="header-image">
13
</header>
14
15
<!-- Input section -->
16
<section class="input-section">
17
<p class="text-lg">Fill your task manager with tasks:</p>
18
<input type="text" id="input-text" class="input-text">
19
<button class="btn-lg" id="submit-btn">Submit</button>
20
</section>
21
22
<!-- Showcase -->
23
<div class="showcase">
24
<ul id="showcase-ul-list">
25
<div class="showcase-li-list" id="showcase-li-list">
26
<li>
27
<button><i class="fas fa-check-circle check" id="check-checked"></i></button>
28
<p>Fill your task manager</p>
29
<button class="trash"><i class="fas fa-trash" id="trash-delete"></i></button>
30
</li>
31
</div>
32
</ul>
33
</div>
34
</div>
35
</body>
Advertisement
Answer
Use trashToDo.addEventListener
instead of trashDelete.addEventListener
. Also you do not need to get removeParent
. Use divToDo.remove();
inside removeTask
.
Issue with your code is due to document.getElementById('trash-delete')
. getElementById
will always find first
element with id trash-delete
. And you are assigning event to that first element only. So it always deletes first one for you.
JavaScript
1
9
1
// Remove task
2
// const trashDelete = document.getElementById('trash-delete'); <-- Remove this line
3
trashToDo.addEventListener('click', removeTask);
4
// Remove below line.
5
// const removeParent = trashDelete.parentElement.parentElement.parentElement;
6
function removeTask() {
7
divToDo.remove();
8
}
9