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

230
Visualizações
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 à 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