I’m attempting to create a To Do List and i’m struggling to stop a button from being created multiple times on the same element in my removeToDo function.
I tried to put the creating of the button into an if statement but struggled to define the condition of the statement to make it only append once.
function addToDo () {
if(document.querySelector('#input').value==false){
alert('Please enter a To Do')
}
else{
var parent = document.getElementById("list");
var newLi = document.createElement("li");
newLi.className = 'toDos';
parent.appendChild(newLi);
var toDo = document.querySelector('#input').value;
newLi.innerText= (toDo);
document.querySelector('#input').value='';
removeToDo();
}
}
function removeToDo () {
var x = document.getElementsByClassName('toDos')
var i;
for (i = 0; i < x.length; i++) {
var remove = document.createElement('button');
remove.className = 'removeButton';
remove.innerText = 'X';
x[i].appendChild(remove)
}
}
New contributor
Burning is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.