Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

140
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda