1
I took this example of menu
$(function(){
var menu = $('.menu-navigation-round');
menu.slicknav();
// Mark the clicked item as selected
menu.on('click', 'a', function(){
var a = $(this);
a.siblings().removeClass('selected');
a.addClass('selected');
});
});
<nav class="menu-navigation-round">
<a href="/" class="selected"><i class="fas fa-home"></i> Home</a>
<a href="/search"><i class="fas fa-search"></i> Busca</a>
<a href="#"> Anuncie seu imóvel</a>
<a href="#"><i class="fas fa-envelope"></i>Contato</a>
<a href="#"><i class="fas fa-users"></i> Sobre nós</a>
</nav>
The Selected class always goes back to the source if I change pages. When using # the code works normally and is selected the correct one, but if I press Search for example, which goes to another page, the menu selects it but in the page update it is again marked the Home option again.
EDIT
<div class="user-menu alignright">
{$page = 'busca'}
<nav class="menu-navigation-round">
<a href="/" {if="$page == 'home'"}Class="selected"{else}""{/if}><i class="fas fa-home"></i> Home</a>
<a href="/search" {if="$page == 'busca'"}Class="selected"{else}""{/if} ><i class="fas fa-search"></i> Busca</a>
<a href="#" {if="$page == 'anuncie'"}Class="selected"{else}""{/if} ><i class="fa fa-bullhorn" aria-hidden="true"></i> Anuncie seu imóvel</a>
<a href="#" {if="$page == 'contato'"}Class="selected"{else}""{/if} ><i class="fas fa-envelope"></i> Contato</a>
<a href="#" {if="$page == 'sobre'"}Class="selected"{else}""{/if} ><i class="fas fa-users"></i> Sobre nós</a>
</nav>
</div>
I made this change now if the page variable is equal to the name of the url it puts the class Selected, and works fine, but I do not know how to change the value of the page variable, if it were in JS it would be the onclick, but how do I do in php this? or in any way that works.
As it was made, it is structured to SPA. When loading the DOM, the first link already comes with the class
.selected
. What you need is to load the class ona
for the loaded DOM. Or make a Javascript function to be executed when the DOM is fully loaded and add the class to thea
correct, relying on the URI... For a large application, the second option will be nothing dynamic...– LipESprY
It assigns the value to a variable in PHP and I made it add the class "Selected", now I just need to change the value of the variable $page, I’m not able to think of a way :S
– Daniel Santos