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

131
Visualizações
How do I make 'mouseleave' event not trigger when hovering over datalist?

I have a sidebar on my page containing a form and some filters. The sidebar is initially collapsed and can be expanded when hovering over via a 'mouseenter' event, and will collapse when the mouse leaves the sidebar via a 'mouseover' event. This seems to work well, but I had an error when trying to add a input field in my form containing a datalist with a list of text options. The problem now is that when I hover over the options, the sidebar will trigger a 'mouseleave' event and collapse, even though the datalist/option elements are a child of the sidebar element. Is there a way to prevent the sidebar from collapsing or am I better off using select instead of datalist, since the select tag seems to not trigger 'mouseleave'?

let sidebar = document.getElementById("sidebar");
sidebar.addEventListener("mouseenter", function(e) {
  toggleSidebar(e, BODY_CONTENT);
});
sidebar.addEventListener("mouseleave", function(e) {
  toggleSidebar(e, BODY_CONTENT);
});

function toggleSidebar(e, bodyContent) {
  if (e.type == "mouseenter") {
    e.target.style.width = e.target.dataset.expandWidth + "px";
    document.getElementById(bodyContent).style.marginLeft =
      e.target.dataset.expandWidth + "px";
  } else if (e.type == "mouseleave") {
    console.log(e);
    e.target.style.width = e.target.dataset.collapseWidth + "px";
    document.getElementById(bodyContent).style.marginLeft =
      parseInt(e.target.dataset.collapseWidth) + 10 + "px";
  }
}
<div class="sidebar" id="sidebar" data-collapse-width="50" data-expand-width="300" style="width: 50px;">
  <div id="flex-sidebar">
    <div class="sidebar-header">
      <h2>
        Filters
      </h2>
    </div>
    <form action="" method="POST" autocomplete="off">
      <div class="form-group">
        <label for="category-filter">Category:</label>
        <input class="form-control me-2" type="search" placeholder="Filter Category" name="category-filter" value="" list="test">
        <datalist class="sidebar-dropdown" id="test">
          <option value="Option 1">1</option>
          <option value="Option 2">2</option>
        </datalist>
      </div>
    </form>
  </div>
</div>

about 4 years ago · Juan Pablo Isaza
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