2
I would like the checkboxes that have been marked to be saved and loaded whenever the page is reloaded or opened again, in addition, in their place there will be icons referring to the options.
I made a function in java script that loads the functions when the page opens, using localStorage but is not working. Could you help me please?
new html.
<div class=" tab-pane active grid-container" id="biblio" align="center">
<div id= "consulta0" class="toggle div-inline" style="display:" >
<input type="checkbox" id="consultar-acervo" data-id="consultar-acervo" name="toggle">
<label for="consultar-acervo"></label>
<p class= "nomeToogle"> Consultar Acervos</p>
</div>
<div id= "consulta1" class="toggle div-inline" style="display:" >
<input type="checkbox" id="consultar-reserva" data-id="consultar-reserva" name="toggle">
<label for="consultar-reserva"></label>
<p class= "nomeToogle"> Consultar Reservas</p>
</div>
<div id= "consulta2" class="toggle div-inline" style="display: ">
<input type="checkbox" id="renovar-emprestimo" data-id="renovar-emprestimo" name="toggle">
<label for="renovar-emprestimo"></label>
<p class= "nomeToogle">Renovar Empréstimos</p>
</div>
<!-- Icones de Biblioteca-->
<div id = "zoom0" class="div-inline zoom" style="display:none">
<a href="https://siga.ufvjm.edu.br/index.php?module=biblioteca&mode=compatibilidade&action=main:pesquisasimples" data-toggle="tooltip" data-placement="top" title="Consultar Acervo" target=“_blank”>
<img src="../images/biblioteca/consultar-acervo.png" style="width:40%" data-id="consultar-acervo" name="icons">
</a>
<p class= "nomeToogle"> Consultar Acervos</p>
</div>
<div id = "zoom1" class="div-inline zoom" style="display:none">
<a href="http://siga.ufvjm.edu.br/index.php?module=biblioteca&mode=compatibilidade&action=main:resusuario" data-toggle="tooltip" data-placement="top" title="Consultar Reserva" target=“_blank”>
<img src="../images/biblioteca/consultar-reserva.png" style="width:40%" data-id="consultar-reserva" name="icons">
</a>
<p class= "nomeToogle"> Consultar Reservas</p>
</div>
<div id = "zoom2" class="div-inline zoom" style="display:none">
<a href="http://siga.ufvjm.edu.br/index.php?module=biblioteca&mode=compatibilidade&action=main:renovusuario" data-toggle="tooltip" data-placement="top" title="Renovar Empréstimo" target=“_blank”>
<img src="../images/biblioteca/renovar-emprestimo.png" style="width:40%" data-id="renovar-emprestimo" name="icons">
</a>
<p class= "nomeToogle"> Renovar Empréstimo</p>
</div>
</div>
functions.js
window.onload = function() { //executa o JavaScript imediatamente após a página ser carregada
function checar(){
var checa = document.getElementsByName("toggle");
var img = document.getElementsByName("icons");
var numElementos = checa.length;
var i = 0;
while (i <= numElementos){
if(checa[i].checked == true){
var dataCheckbox = checa[i].dataset.id;
var dataImage = img[i].dataset.id;
if (dataCheckbox[i] == dataImage[i]){
document.getElementById("zoom"+i).style.display = 'inline-block'; // mostra imagem
document.getElementById("consulta"+i).style.display = 'none'; // oculta checkbox marcada
document.getElementById("aplica").style.display = 'none'; // oculta botão aplicar
document.getElementById("edita").style.display = 'inline'; // mostra botão editar
document.getElementById("cancela").style.display = 'none'; // oculta botão cancela
}
//a partir daqui salva os checkbox marcados pelos usuários
var opc = localStorage.getItem("checa[" + i + "]"); //verifica se há alguma opc no localStorage
if (opc == null){
break;
}
alert("deu");
//grava as opc no localStorage
localStorage.setItem("checa["+ indice +"]", checa);
localStorage.setItem("img["+ indice +"]", img);
indice++
}
else{
document.getElementById("consulta"+i).style.display = 'none'; // oculta checkbox marcada
}
i++;
}
}
document.getElementById('aplica').onclick = checar;
//função que carrega os ícones na tela de acordo com as opções salvas
var checa = document.getElementsByName("toggle");
var img = document.getElementsByName("icons");
var numElementos = checa.length;
var b=0;
for (i=0; i <= numElementos; i++){
var opc = localStorage.getItem("checa[" + i +"]"); //verifica se há opc
if(opc == null){
break;
}
var dataCheckbox = checa[i].dataset.id;
var dataImage = img[i].dataset.id;
if (dataCheckbox[i] == dataImage[i]){
document.getElementById("zoom"+i).style.display = 'none'; // oculta imagem
document.getElementById("consulta"+i).style.display = 'inline-block'; // mostra checkbox marcada
document.getElementById("aplica").style.display = 'block'; // mostra botão aplicar
document.getElementById("edita").style.display = 'none'; // oculta botão editar
document.getElementById("cancela").style.display = 'inline-block'; // mostra botão cancela
}
else{
document.getElementById("consulta"+i).style.display = 'inline-block'; // mostra checkbox marcada
}
}
};
Do not create a localStorage for each item... create only 1 and save the options in string format.
– Sam
Thanks for the suggestion, dvd, I searched for it here in the forum and I found a solution given by you in another topic. I tried to modify to my context and add, in the check() function, only it still doesn’t work, look how it turned out:
– MStos
Document.addeventlistener("Domcontentloaded", Function(){ var checkbox = Document.querySelectorAll("input[type='checkbox']"); for(var itemCheck){ item.addeventlistener("click", Function(){ localStorage.query ? localStorage.query = localStorage.consulta.indexof(this.id+",") == -1 ? localStorage.query+this.id+"," : localStorage.consulta.replace(this.id+",","") : localStorage.query = this.id+","; }); }
– MStos
if(localStorage.query){ for(var itemCheck){ item.checked = localStorage.consulta.indexof(item.id+",") != -1 ? true : false; } } });
– MStos
No, I’m using javascript. With regard to the images, it goes like this: after the user chooses the checkbox, I check the ones that are marked and replace them with icons referring to the functionalities, the ones that were not marked tbm are hidden from the screen, but they will not have their icons shown. That function I’ve done, just tried to use it to save those chosen checks.
– MStos