Skip to content
Advertisement

Remove parent node on click of button throw function on found error

I’m working on my companies legacy project. It uses jQuery. I have to add a feature to it. The feature is that, on clicking a checkbox, a span with the checked value (let’s call it “filtered value”) will be displayed with a “remove” icon in it. On clicking the remove icon of the “filtered value”, filtered value should be removed and corresponding checkbox should be unchecked.

I got the first half working. But REMOVE functionality isn’t working. I get an error “ReferenceError, removeFilter is not defined.” Here is the Codesandbox.

JavaScript

Advertisement

Answer

Several problems:

  1. removeFilter() needs to be declared globally, i.e. outside the “on change” handler.
  2. You need to pass the correct ID to removeFilter().
  3. removeFilter() needs to remove the parent <span> as well as uncheck the corresponding checkbox.
  4. When clicking the “X” to remove a filter, you need to remove it from the filtersApplied array.

Here’s a working model:

JavaScript
JavaScript
JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement