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

116
Views
callback function with addeventlistener

I'm putting my addeventlistener function inside other appendingEvents function, cause some of the variables got created here. But i want to make my appendingEvents function not messy.

function appendingEvents() {
  for (let i = 0; i < eventFullInfo.title.length; i++) {
    let classes = document.querySelector(eventFullInfo.class[i]);

      document.querySelector(`.delete-${eventFullInfo.class[i].substring(1)}`).addEventListener("click", function () {
      
        classes.innerHTML = "";
        eventFullInfo.class.splice(i, 1);
        eventFullInfo.title.splice(i, 1);
        eventFullInfo.date.splice(i, 1);
        eventFullInfo.type.splice(i, 1);
        eventFullInfo.desc.splice(i, 1);
        eventFullInfo.etime.splice(i, 1);
        eventFullInfo.stime.splice(i, 1);      
    });
    
  }
}

So i'm trying to make a function outside the appendingEvents functions, and just call it with addeventlistener function.

function appendingEvents() {
  for (let i = 0; i < eventFullInfo.title.length; i++) {
    let classes = document.querySelector(eventFullInfo.class[i]);
    
    document.querySelector(`.delete-${eventFullInfo.class[i].substring(1)}`).addEventListener("click", deleteEvent(classes, i);
  }
}


function deleteEvent(classes, i) {
        classes.innerHTML = "";
        eventFullInfo.class.splice(i, 1);
        eventFullInfo.title.splice(i, 1);
        eventFullInfo.date.splice(i, 1);
        eventFullInfo.type.splice(i, 1);
        eventFullInfo.desc.splice(i, 1);
        eventFullInfo.etime.splice(i, 1);
        eventFullInfo.stime.splice(i, 1);

}

How this is supposed to look like? Cause created function above doesn't work.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

addEventListener takes a function as its second argument. What you are doing is calling the deleteEvent function and putting the result of the function there instead.

You can create an anonymous function that gets triggered instead and call deleteEvent from within that. You can then pass whatever you want into it.

function appendingEvents() {
  for (let i = 0; i < eventFullInfo.title.length; i++) {
    let classes = document.querySelector(eventFullInfo.class[i]);

    document.querySelector(`.delete-${eventFullInfo.class[i].substring(1)}`).addEventListener("click", () => {
      deleteEvent(classes, i)
    })
  }
}


function deleteEvent(classes, i) {
  classes.innerHTML = "";
  eventFullInfo.class.splice(i, 1);
  eventFullInfo.title.splice(i, 1);
  eventFullInfo.date.splice(i, 1);
  eventFullInfo.type.splice(i, 1);
  eventFullInfo.desc.splice(i, 1);
  eventFullInfo.etime.splice(i, 1);
  eventFullInfo.stime.splice(i, 1);

}

about 4 years ago · Juan Pablo Isaza Report

0

The event listener callback function has one argument that is passed: the event object. The easiest way to pass custom data is creating an anonymous function which invokes your function.

function appendingEvents() {
  for (let i = 0; i < eventFullInfo.title.length; i++) {
    let classes = document.querySelector(eventFullInfo.class[i]);
    
    document.querySelector(`.delete-${eventFullInfo.class[i].substring(1)}`).addEventListener("click", function(){ deleteEvent(classes, i) });
  }
}

An alternate solution is to use .bind().

function appendingEvents() {
  for (let i = 0; i < eventFullInfo.title.length; i++) {
    let classes = document.querySelector(eventFullInfo.class[i]);
    
    document.querySelector(`.delete-${eventFullInfo.class[i].substring(1)}`).addEventListener("click", deleteEvent.bind(null, classes, i));
  }
}
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!