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

191
Vistas
Javascript attribute - onClick and onClickOverThere for dropDown

I have reactJs app and I have made a custom dropDown with a div that I set an onClick attribute to open dropDown and close it. but also I want to close it when the user clicks to another part of the site.

 <div
    onClick={() => setNoneQuote(noneQuote ? false : true)}
    className="selected-drop-down"
 >
    <span className="dropDownText">{selectedQuoteCurrency}</span>
    <img
      className="dropDownIcon"
      src={require("../assets/image/arrow/dropDown-Arrow.png")}
      width="15px"
      alt="arrow"
    />
 </div>

I try onMouseDown instead of onClick according to this answer ==> stackoverflow ,but I don't know why it doesn't work for me :(

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

const dropdownElement= document.querySelector("choose an id or a specific class")
window.onclick = function(event) {
  if (event.target !== dropdownElement) {
    dropdownElement.style.display = "none"; // or any other function that you want to call
  }
}

this may not be the exact code to fix your problem but you can use the logic

about 4 years ago · Juan Pablo Isaza Denunciar

0

You could do it in different ways, but I will show you solution that I used in one of my projects.

So, I used <header> and <main> tags where I had all my Components. And in those tags i used eventListener with a callback function, like this:

<header onClick={handleClick}>
      <Nav />
      <Greeting />
      <Sidebar />
</header>

and

<main onClick={handleClick}>
          <Description />
          <Icons />
          <Prices />
          <Gallery />
          <NecessaryInfo />
          <Location />
          <GalleryModal />
          <ConditionsModal />
        </main>

In a callback function handleClick I checked where user could click and to do that I used next logic of pure JS:

!e.target.classList.contains("menu-open") &&
  !e.target.classList.contains("menu-links") &&
   !e.target.parentElement.classList.contains("menu-links") &&
  !e.target.parentElement.parentElement.classList.contains("menu-links") &&
  closeSidebar();

Function closeSidebar() is simple:

const closeSidebar = () => {
setIsSidebarOpen(false)};

In your code instead of using setNoneQuote(noneQuote ? false : true) you could also use: setNoneQuote(!noneQuote). Exclamation mark before value always will change it to the oposite.

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