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

227
Vistas
How to remove Event Listener using MutationObserver?

I have a problem, I can't remove the event listener in the condition inside the callback function.

I have the impression that the syntax is correct. But the event is still active.

Does anyone know how to remove the event touchstart?

mutation.target.removeEventListener("touchstart",(e) => { handleClickButton(e) }, true);
const headerSearchBox = document.querySelector(".search-box");

function handleClickButton(event) {
    event.preventDefault();
    alert("You Clicked on the button")
}
    
const mutationCallback = function (mutationList) {
  mutationList.forEach((mutation) => {
    let isSearchOpen = mutation.target.classList.contains("search-container");
    
    // If Search Bar is Open : Do This...
    if (mutation.type === "attributes" && isSearchOpen && mutation.oldValue.includes("open")) {
      console.log("Search Bar is Open");
      mutation.target.addEventListener("touchstart",(e) => { handleClickButton(e) }, true);
    } else {
      console.log("Search Bar is Close");
      mutation.target.removeEventListener("touchstart",(e) => { handleClickButton(e) }, true);
    }
  });
};

const observer = new MutationObserver(mutationCallback);

observer.observe(headerSearchBox, {
  attributes: true,
  attributeOldValue: true,
  attributeFilter: ["class"],
});

Thanks for your support

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