Andrés Girón Andrés Girón - 1 month ago 5
HTML Question

Show hidden div permanently throughout website after clicking on a specific <a> href link once

i found this link of a question like the one i have, the thing is that the solution they give its for clicking any div in the html, and i need something like, when i click the first div, the second div its showed and when i press the second div, the third div its showed, and i need it to keep on localstorage... This is my HTML code:

<div id='btn1' class="col-lg-4"">
<a id="tema_1 " href="tema.html"><img id="img_tema1" class="img-circle" src="../assets/img/primer_tema.gif" alt="Generic placeholder image" width="140" height="140"></a>
<h2>Tema 1</h2>
</div><!-- /.col-lg-4 -->

<div class="col-lg-4-2">
<a href=""><img id="img_tema2" class="img-circle2" src="../assets/img/segundo_tema.gif" alt="Generic placeholder image" width="140" height="140"></a>
<h2>Tema 2</h2>
</div><!-- /.col-lg-4-2 -->

<div class="col-lg-4-3">
<a href="tema_3.html"><img id="img_tema3" class="img-circle3" src="../assets/img/tercer_tema.gif" alt="Generic placeholder image" width="140" height="140"></a>
<h2>Tema 3</h2>
</div><!-- /.col-lg-4-3 --


And this is the example of Jquery code that i use:

var hide2 = localStorage[location] ? false : true;
var hidden2 = document.querySelector('.col-lg-4-2');

if(hide2) {
hidden2.style.display = 'none';
document.onclick = function() {
localStorage[location] = true;
hidden2.style.display = '';
document.onclick = '';
console.log('click');
}
}


But as i say... it makes that any div that i click, shows the Tema 2, and i need that the only div that can show the Tema 2 is the Tema 1 Div.

Excuse my bad english but my mother language its Spanish.

Thank you for any help.

Answer

I think this might be solved using simple jQuery. I've used the example specified at: Previous Similar question and changed it so it will fit your code. It currently works which is fine but I might wanna work around it if you don't like my solution.

 $(function () {
    var showLittleHeader = localStorage.getItem('#second-img');
    if (showLittleHeader) {
        $('#second-img').show();
    }
    $('#first-img').on('click', function () {
        localStorage.setItem('#second-img', 1);
        $('#second-img').show();
    });

});

$(function () {
    var showLittleHeader = localStorage.getItem('#third-img');
    if (showLittleHeader) {
        $('#third-img').show();
    }
    $('#second-img').on('click', function () {
        localStorage.setItem('#third-img', 1);
        $('#third-img').show();
    });

});

Look at this JS Fiddle and see if it does the job for you:

Updated Solution with Local Storage

Comments