job = document.getElementById("job")
job.addEventListener('click', addJob)
jobInput = document.getElementById("jobInput")
breakObject = document.getElementById("break")
cnt = 1
function addJob() {
breakObject.insertAdjacentHTML('afterEnd', "</div>")
breakObject.insertAdjacentHTML('afterEnd', "<button class='delete'> Delete </button>")
temp = "<p>".concat(jobInput.value, "</p>")
breakObject.insertAdjacentHTML('afterEnd', temp)
breakObject.insertAdjacentHTML('afterEnd', "<br>")
breakObject.insertAdjacentHTML('afterEnd', "<div id='del".concat(cnt.toString, "'>"))
document.getElementById('del'.concat(cnt.toString)).addEventListener('click', deleteObj(document.getElementById('del'.concat(cnt.toString))))
cnt = cnt+1
}
function deleteObj(obj) {
obj.remove()
console.log("debug")
}
.specialButton {
background-color: navy;
color: white;
height: 25px;
width: 75px;
border-radius: 10px;
border: none;
transition-duration: 0.3s;
}
.specialButton:hover {
color: navy;
background-color: white;
border: solid;
border-color: navy;
}
p {
font-family: "Lucida Console", monospace;
color: yellow;
}
.delete {
width: 60px;
height: 20px;
background-color: red;
}
.delete:hover {
width: 60px;
height: 20px;
border-color: red;
color: red
}
<div id="jobList">
<input id="jobInput" type="text">
<button class="specialButton" id="job">Add </button>
</div>
<br id="break">
Any idea why my todo list isn’t working? I know that the concat delete place is not very clear but please help me to code one that actually works. I don’t know any other way that can get this to work so please help me. Thanks in advance.
2
Answers
It is much more convenient to create elements with
document.createElement
rather than manipulating HTML strings.As @Unmitigated said , it convenient to use document.createElement and you are calling the delete function on eventlistener immediately