0
I have this code that makes the menu active and expands.
<li class="menu-item">
<a href="#" class"active">
<span>Posts</span>
</a>
<ul class="sub-menu">
<li><a href="#">Ver Posts</a></li>
<li><a href="#">Novo Post</a></li>
</ul>
</li>
$(".menu-item > a").click(function(){
$(".sub-menu").slideUp();
if(!$(this).next().is(":visible")) {
$(this).next().slideDown();
}
$('.active').removeClass('active');
$(this).addClass('active');
localStorage.setItem("activeDiv", $(this).index('.menu-item > a'));
});
var activeIndex = localStorage.getItem("activeDiv");
if (activeIndex) {
$('.menu-item').removeClass('active').eq(activeIndex).addClass('active');
}
How I do for when I click on the "View Posts or New Post" submenu continue opening after page refresh?
It worked! But I tried to add other links and when it’s true, everyone stays open.
– Fernando
Could you show an example in Jsfiddle? It’s easier to see what’s going on. You can use the same file I passed as an example. Just edit it, press it Update and give me the link
– Lucas Nascimento
I don’t know if it solves your case, but check out the following example. I added a new Key activeDiv2 and did the same in the previous example. https://jsfiddle.net/30mh3syx/5/
– Lucas Nascimento
In the Posts and Categories menu, note that when the true is set when refreshing the page the 2 are open and the active class does not remain. https://jsfiddle.net/30mh3syx/6/
– Fernando
This is because the check
if (JSON.parse(activeIndex) == 'true') {
 $('.sub-menu').show();
show() in every element with Class sub-menu when activeIndex is true. In the following example I have made the situation by creating new classes, a new key and adding a new check: https://jsfiddle.net/30mh3syx/5/– Lucas Nascimento