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

88
Visualizações
onclick on search bar

I am trying to hide a container when the search bar is clicked but the page reloads and the container is shown again.

the code is below

<div class="search">
        <form action="" method="GET">
        <input type="text" name="search" value="" placeholder="search member..">
            <button class="search_btn">
                <i class="fas fa-search" style="font-size: 18px;"></i>
            </button>
        </form>
    </div>

the javascript for adding the class of hide

    const search_btn=document.querySelector(".search_btn");
const content=document.querySelector(".content");

search_btn.addEventListener("click", search);

let se=false;
function search(){
    if(!se){
        content.classList.add("hide");
        se=true;
    }else{
        content.classList.remove("hide");
        se=false;
    }
}

and the css part of itis as below

      .container .content.hide{
    display:none;
  }   
.container .content{  
display: block;
    margin: 20px;
    background: #f2f5f9;
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The page is reloading becase your code contains form and when you are clicking on the search you are actually submit the form. You can use event.preventDefault() for prevent it.

function search(event){
    if(!se){
        content.classList.add("hide");
        se=true;
    }else{
        content.classList.remove("hide");
        se=false;
    }
 event.preventDefault()
}
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