Here the complete code.
The html:
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="css/main.css">
<link href="https://fonts.googleapis.com/css?family=Roboto&display=swap" rel="stylesheet">
<link rel="icon" href="img/list.png">
<title>Check List</title>
</head>
<body>
<div class="main">
<form action="#">
<div class="new">
<input type="text" id="texto">
<button id="btn">add</button>
<img src="img/reload.png" onclick="location.reload();">
</div>
<div class="lista">
<ol class="listagem" id="listagem">
</ol>
</div>
</form>
</div>
<script src="js/js.js"></script>
</body>
</html>
The js:
var listagem = document.getElementById("listagem");
function createNewElement() {
// Cria i Para receber o icon
var icon = document.createElement("i");
icon.setAttribute("id", "icon");
// Cria a div
var div = document.createElement("div");
div.setAttribute("id", "d");
// Cria a li
var li = document.createElement("li");
li.setAttribute("id", "li");
// Cria o botão
var check = document.createElement("input");
check.setAttribute("id", "check");
check.type = "checkbox";
// Pega o valor do input digitado
var texto = document.getElementById("texto").value;
li.innerText = texto;
// Adiciona a lista
div.appendChild(check);
li.appendChild(icon);
div.appendChild(li);
listagem.appendChild(div);
}
function verificarCheck() {
var li = document.querySelectorAll("li");
var check = document.querySelectorAll("input#check");
for (let i = 0; i < check.length; i++) {
for (let i = 0; i < li.length; i++) {
if (check[i].checked) {
check[i].style.display = "block";
li[i].style.textDecoration = "line-through";
//icon[i].setAttribute("class", "material-icons");
//icon[i].innerText = "add";
} else {
// Nada
}
}
}
}
setInterval(verificarCheck, 100);
const btn = document.getElementById("btn");
btn.addEventListener("click", function() {
var li = document.querySelectorAll("li");
var texto = document.getElementById("texto").value;
if (texto) {
createNewElement();
document.getElementById("texto").value = "";
} else {
// Nada
}
});
If I am not mistaken the information that these elements have already been created should be stored in some way, as in a
LocalStorage
to generate the elements when the page is loaded, if previously generated.– Pedro Roweder
Obg, but is there any way you can show me an example? (Because I’m a "beginner")
– Dominik
Puts HTML in question.
– Sam
I just put html and js down there.
– Dominik