Yes, it is possible to save the data in the browser with localStorage
. Saved information is stored permanently in the browser until you delete it.
To delete what was typed in the input after clicking "Add", simply call the function clearFields()
at the end of the function that adds (myFunction()
).
A very functional way is to use another function to make the control of the localStorage
. Know that the localStorage
(LS) only stores strings, so the solution I suggest is to create an array of <li>
from the list and save to LS. Then just recover the saved string, convert to array again with .split()
and make a loop by creating the list HTML and inserting it into ul
. The code below makes everything work (see the explanatory comments):
I did not create a snippet because it does not accept localStorage.
HTML
<input id="retorno" name="texto" type="text" placeholder="Add item here...">
<button id="botao" onclick="myFunction()" type="button">Add</button>
<button type ="button" onclick="clearFields();">Clear</button>
<button type="button" onclick = "deleteField();">Delete</button>
<ul id="todolist"></ul>
Javascript
// controla o localStorage
function ls(e){
if(e){
// aqui grava o localStorage
var array = []; // cria a array
var lista = document.querySelectorAll("#todolist li"); // pega todas as <li>
for(var item of lista){ // percorre as <li>
array.push(item.textContent); // insere os valores na array
}
localStorage.setItem("dados", array);
}else{
// aqui retorna
return localStorage.getItem("dados");
}
}
function myFunction(){
var test = document.getElementById("retorno").value;
var listValue = document.createTextNode(test);
var cre = document.createElement("LI");
document.getElementById("todolist").appendChild(cre);
cre.appendChild(listValue);
clearFields(); // limpa o input
ls(true);
};
function clearFields(){
document.getElementById("retorno").value = "";
document.getElementById("retorno").focus(); // coloca o foco no input
}
function deleteField(){
var del = document.getElementById("todolist");
if (del.hasChildNodes()){
del.removeChild(del.lastChild);
ls(true);
}else{
localStorage.removeItem("dados"); // apaga o localStorage
}
}
// aqui quando carrega a página
// verifica se há algo no localStorage "dados"
if(ls()){
var lis = ''; // variável vazia
var ls_array = ls().split(","); // converte o localStorage em array
for(var item of ls_array){
lis += '<li>'+item+'</li>'; // cria o HTML das <li>
}
document.getElementById("todolist").innerHTML = lis; // insere o HTML na ul
}
Both questions and answers are formatted with Markdown. Read Help with Markdown editing
– Augusto Vasques
Yes, it is. Links that can help you: https://answall.com/q/413997/99718 and https://answall.com/a/90597/99718
– Valdeir Psr
@Cypherpotato, even editing the question, has two different questions. I should flag it, but I think this question is very good to talk more about Javascript storage. Now, how do you do it ?
– JeanExtreme002
@Jeanextreme002 answers the main question if you have one and notifies you to create another question for each of the others. If all questions asked have the same focus and different contexts, vote to close with "Too wide".
– CypherPotato