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

103
Visualizações
Javascript firing after two attempts

Ok maybe the title is worded a bit wrong...

I have some javascript that is supposed to open my sidebar once the button is clicked - But it is taking two clicks to open the sidebar and another two clicks to close..

I understand why this is happening via my Javascript - But not sure on the remedy (Not sure if its actually a Blazor issue?)

Javascript

function LoadContent() {

    document.addEventListener("DOMContentLoaded",
        document.onclick = function(event) {

            const showNavbar = (toggleId, navId, pId, headerId) => {
                const toggle = document.getElementById(toggleId),
                    nav = document.getElementById(navId),
                    bodypd = document.getElementById(pId),
                    headerpd = document.getElementById(headerId);
                    mainbodypd = document.getElementById("mainbody");
                

                if (toggle && nav && bodypd && headerpd && mainbodypd) {
                    toggle.addEventListener("click",
                        () => {
                            nav.classList.toggle("show");
                            toggle.classList.toggle("bx-x");
                            bodypd.classList.toggle("body-pd");
                            headerpd.classList.toggle("body-pd");
                            mainbodypd.classList.toggle("height-100short");

                        });
                }
            };

            showNavbar("header-toggle", "nav-bar", "body-pd", "header");

            const linkColor = document.querySelectorAll(".nav_link");

            function colorLink() {
                if (linkColor) {
                    linkColor.forEach(l => l.classList.remove("active"));
                    this.classList.add("active");
                }
            }

            linkColor.forEach(l => l.addEventListener("click", colorLink));

        });
   
}

Can anyone explain, how i can prevent this from happening (Sorry, i am fairly new to JS)

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

0

So why are you doing:

document.onclick = function(event) {

This makes the function trigger every page click ANYWHERE on the page (meaning you keep adding event listeners every time you click).

You should probably remove that part, or if you need it for something, take the shownavbar section out of it and just have it in DOMContentLoaded

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