1
I have a project and I’m using localStorage
as a database. I use this to save a list that the user can edit and add like this:
localStorage.setItem(local, $("#lista").html());
In html it 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 a array.
When it saves, the navigator shoves html in itself... and puts class there read. ai 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 full before the split:
,test1,,teste2,teste3,,teste4,
Then I use teste[1]
or teste[3]
it returns correctly. When use teste[2]
he returns nothing.
If anyone has any 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 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);
}
Post an excerpt of code that you can reproduce there, otherwise we will have to write an HTML, function and etc just to help you... If you can post all your HTML there or something we can copy and play without much work.
– Paulo Roberto
Hi Paulo, I’m new here and I’m lost in this format wave. I had to add a demo page here and put the links.. pq to put the code was formatting in half.. , anyway, take a look.
– Felipe Leão