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

177
Views
How do I add and remove an event listener using a function with event argument and parameters?

For example, I have below function to execute when mouseup event occurs.

const listener = (element: HTMLElement, e: MouseEvent): void => {
    if (e.target instanceof Element && element && !element.contains(e.target)) {
        element.style.backgroundColor = 'red';
    }
}

Then I am trying to figure out how to add extra paramter element to listener and remove it when it is not required anymore.

function addEventListener (element: HTMLElement) => {
    window.document.addEventListener('mouseup', (e) => listener(element, e));
}

function removeEventListener (element: HTMLElement) => {
    window.document.removeEventListener('mouseup', ?? );
}

How can I solve this problem?

I tried using bind like below, but MouseEvent cannot bind before addEventListener.

let callback;

function addEventListener (element: HTMLElement) => {
    // how can I bind `MouseEvent`?
    callback = listener.bind(this, element, ??);
    window.document.addEventListener('mouseup', callback);
}

function removeEventListener (element: HTMLElement) => {
    window.document.removeEventListener('mouseup', callback);
}

Should I use closure here like below?

function createListener(element: HTMLElement) {
    return (e: MouseEvent) => {
        listener(element, e);
    }
}

let callback;

function addEventListener (element: HTMLElement) {
    callback = createListener(account);
    window.document.addEventListener('mouseup', callback);
}

function removeEventListener (element: HTMLElement) => {
    window.document.removeEventListener('mouseup', callback);
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The same way you remove an event listener normally, but store a Map (dictionary) that maps an HTMLElement to a function:

const callbacks = new Map<HTMLElement, (e: MouseEvent) => void>();

function addEventListener (element: HTMLElement) => {
    const callback = (e) => listener(element, e);
    callbacks.set(element, callback);
    window.document.addEventListener('mouseup', callback);
}

function removeEventListener (element: HTMLElement) => {
    const callback = callbacks.get(element);
    if(callback)
    {
        window.document.removeEventListener('mouseup', callback);
        delete callback;
    }
}
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!