0
I’m studying javascript and developing a Todo List.
The only thing I want to do is add elements to a <ul>
and then enable them to click
to just change their class.
Man html
which is very simple... js
is already a zone because I’ve tried everything and nothing works...
var addBtn = document.querySelector("button");
var inputText = document.getElementsByTagName("input").value;
var inputT = document.getElementById("addLista");
var listaCompleta = document.querySelector("#listaCompleta");
var itensLista = document.getElementsByClassName("notDone");
function updateLista (lista){
return lista
}
var listaOk = updateLista(itensLista)
addBtn.addEventListener("click", function () {
var liAdd = document.getElementById("addLista").value;
if (liAdd !== "") {
var liNova = document.createElement("li");
liNova.textContent = liAdd;
liNova.classList.add("notDone");
listaCompleta.appendChild(liNova);
liLength = itensLista.length
console.log(liLength);
itensLista = document.getElementsByTagName("li");
}
inputT.value = "";
listaOk = updateLista(itensLista)
});
function click (listaOk){
for( var i = 0; i <= listaOk.length; i++){
if(listaOk[i]){
listaOk[i].addEventListener("click", function(){
this.classList.toggle("done");
})
}
}
}
click(listaOk);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link type ="text/css" rel="stylesheet" href="todoStyle.css">
<title>Todo Dom</title>
</head>
<body>
<h1>Todo List</h1>
<div class="central">
<form action="">
Adicione um Item: <input type="text" id="addLista" name="addLista"> <button type="button">add</button></form>
<ul id="listaCompleta">
</ul>
</div>
<script type="text/javascript" src="todoJs.js"></script>
</body>
</html>
Thanks!
Thank you so much for the answer, buddy. It really worked. I just wish I knew what was wrong with my logic using "for".
– Bruno T
Actually I didn’t quite understand the way you were trying to do it, and using
for
will repeat theaddEventListener
with each item addition (which may cause some errors) - so I left it that way to add the listener to the last item added.– BrTkCa
@Brunot, if the answer answered you can mark as accepted :)
– BrTkCa