Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

225
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!