Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

108
Visualizações
Activating the menu working only after two clicks

I have a menu that when I click on something the menu becomes active and is highlighted.

The code works fine, I don't know if it's a problem in the blazor or it's something in my code anyway.

But what's happening is the following, when I open the application and I click once on some of the menus nothing happens, then if I click again the menu starts to work normally with just one click

HTML Code:

<div>
   <ul class="nav" onclick="ActiveTest()">
      <li>
         <a href="#" class="active">
         <i class="fas fa-home"></i>
         <span class="name">Home</span>
         </a>
      </li>
      <li>
         <a href="#">
         <i class="fas fa-book-reader"></i>
         <span class="name">Reader</span>
         </a>
      </li>
      ...
   </ul>
</div>

JS:

function ActiveTest() {
    $(".nav li a").click(function () {
        $(".nav li a").removeClass("active");
        $(this).addClass("active");
    });
}

Anyone who has had this problem can tell me a way to make this work without this two-click issue when the app is opened

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The first click you are calling ActiveTest which then adds the click event to all the <li> in your code.

You probably want something like this instead.

HTML

<div class="bar">
   <ul class="nav">
      <li >
         <a href="#" onclick="OpenMenu(this)" class="active">
         <i class="fas fa-home"></i>
         <span class="name">Home</span>
         </a>
      </li>
      <li>
         <a href="#" onclick="OpenMenu(this)">
         <i class="fas fa-book-reader"></i>
         <span class="name">Reader</span>
         </a>
      </li>
   </ul>
</div>

JS

function OpenMenu(el) {
    
   $(".nav li a").removeClass("active"); 
   $(el).addClass("active");
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda