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

99
Views
jQuery object is empty after dynamically creating it

I want to add an event listener inside a class after creating the corresponding element.

In my event listener inside the appendSelectedFilter function I want to refer to the clicked element. Since I can't use this in this context I tried to create a jQuery object and parse it to the resetThisFilter function, but the object has a length of 0.

What am I missing here? How can I parse the clicked object to another function?

class RecipeFilter {
  constructor(filterkategories, response) {
    this.filterkategories = filterkategories;
    this.response = response;
  }

  appendSelectedFilter(filterItem) {
    let value = filterItem.children[0].children[0].value;
    let filter = `<div class="selectedFilter ${value}" ><span class="icon-pk-close"></span>${value}</div>`;
    if ($(".checked-filter").find(`.${value}`).length === 0) {
      $(".checked-filter").prepend(filter);
    }
    $(document).on("click", ".selectedFilter", () => {
      this.resetThisFilter($(`.selectedFilter .${value}`));
    });
  }

  var recipeFilter = new RecipeFilter(filterkategories, response);
  recipeFilter.renderFilters(filterkategories);

  $(document).on("click", ".filter-list-item", function() {
    recipeFilter.appendSelectedFilter(this);
  });
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Accept the Event object as an argument to your arrow function, then you can use the target property of the event to retrieve the element which the user clicked on:

appendSelectedFilter(filterItem) {
  let value = filterItem.children[0].children[0].value;
  let filter = `<div class="selectedFilter ${value}" ><span class="icon-pk-close"></span>${value}</div>`;
    
  if ($(".checked-filter").find(`.${value}`).length === 0) {
    $(".checked-filter").prepend(filter);
  }
    
  $(document).on("click", ".selectedFilter", e => {
    this.resetThisFilter($(e.target));
  });
}
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!