I have a project and I'm using localStorage
as the database. I use this to save a list that the user can edit and add like this:
localStorage.setItem(local, $("#lista").html());
No html looks like this:
<ul data-role="listview" contenteditable="true" id="lista">
<li></li>
</ul>
My need is to retrieve each information between each li
in variables or even an array .
When saved, the browser threads html itself ... and boots class there li. then using:
var teste = lista.split(/<li class="ui-li ui-li-static ui-body-c">|<\/li>|¤/);
To return each value within each li.
Only when recovering using teste[1]
, teste[2]
... works in half.
For example: Value saved in whole before process:
test1, test2, test3, test4,
Then I use teste[1]
or teste[3]
it returns correctly. When I use teste[2]
it does not return anything.
If anyone has a solution. replaceAll
is not compatible with my project.
PAGE THAT RECEIVES THE VALUE:
<!DOCTYPE HTML>
<head>
<script src="http://code.jquery.com/jquery-1.7.1.min.js"type="text/javascript"></script>
<script type="text/javascript" src="teste.js"></script>
</head>
<body>
<button onClick="att()">Mostrar</button>
<div id="boxgeral">BOX GERAL</div>
</body>
</html>
PAGE THAT SAVES THE VALUE:
<!DOCTYPE HTML>
<head>
<script src="http://code.jquery.com/jquery-1.7.1.min.js"type="text/javascript"></script>
<script type="text/javascript" src="teste.js"></script>
</head>
<body>
<input type="text" name="titulo2" id="titulo2" value="" placeholder="Titulo" />
<h5> Opções: </h5>
<ul data-role="listview" contenteditable="true" id="lista">
<li></li>
</ul>
<button id="salvar">Salvar</button>
<button id="limpar">Deletar</button>
</body>
</html>
Javascript code:
var pag = 'pag1';
var titulo = localStorage.getItem(pag);
var lista = localStorage.getItem(titulo);
$(function () {
$("#salvar").click(function () {
var local = document.getElementById('titulo2').value;
localStorage.setItem(pag, local);
localStorage.setItem(local, $("#lista").html());
});
if (localStorage.getItem(titulo)) {
$("#lista").html(localStorage.getItem(titulo));
document.getElementById('titulo2').value = titulo;
}
$("#limpar").click(function () {
localStorage.removeItem(titulo);
localStorage.removeItem(lista);
alert(localStorage.getItem(titulo));
alert(localStorage.getItem(lista));
window.location = window.location;
});
});
function att() {
var teste = lista.split(/<li class="ui-li ui-li-static ui-body-c">|<\/li>|¤/);
alert(teste);
}