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

334
Views
How to remove a single function from click event listener that has multiple functions, using Javascript?

Using Javascript, how do you remove a single function from a "click" event listener, after the first click event, when there are multiple functions being used on the element with the "click" event listener? Here, after the first click, only toggleView() should remain and renderList() should be removed.

const button = document.getElementById("view_button");
button.addEventListener("click", () => {
  toggleView(); // want to keep this indefinitely
  renderList(); // want to remove this after the first click
  button.removeEventListener("click", renderList);
});
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You need to add both separately, and specify { once: true } on the one you want removed after first execution.

See MDN:

once
A boolean value indicating that the listener should be invoked at most once after being added. If true, the listener would be automatically removed when invoked.

function toggleView() {
  console.log('toggleView');
}

function renderList(evt) {
  console.log('renderList');
}


const button = document.getElementById("view_button");
button.addEventListener("click", toggleView);
button.addEventListener("click", renderList, { once: true });
<button type="button" id="view_button">Click</button>

about 4 years ago · Juan Pablo Isaza Report

0

You can add two event listeners and remove the one

function toggleView() {
  console.log('toggleView');
}

function renderList(evt) {
  evt.currentTarget.removeEventListener("click", renderList);
  console.log('renderList');
}


const button = document.getElementById("view_button");
button.addEventListener("click", toggleView);
button.addEventListener("click", renderList);
<button type="button" id="view_button">Click</button>

or you can just add logic to determine if the function should be run.

function toggleView() {
  console.log('toggleView');
}

function renderList() {
  console.log('renderList');
}


const button = document.getElementById("view_button");
button.addEventListener("click", () => {
  toggleView();
  if (!button.dataset.clicked) {
    renderList();
    button.dataset.clicked = '1';
  }
});
<button type="button" id="view_button">Click</button>

about 4 years ago · Juan Pablo Isaza Report

0

I would use a closure here to keep track of if the item has been clicked.

const button = document.getElementById("view_button");

function toggleView() {
  console.log('Toggle View');
}

function renderList() {
  console.log('Render List');
}

function clickHandler() {
  let hasBeenClicked = false;
  return function () { 
      toggleView();
      if (!hasBeenClicked) {
        renderList();
        hasBeenClicked = true;
     }
  };  
}



button.addEventListener("click", clickHandler());
<button id="view_button" type="button">View Button</button>

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