'Toggle attribute with an if statement with jQuery

I am in the process of programming cookies for our filters. I wanted to use a specific attribute to generate cookies and delete them again when the filter is deselected. Unfortunately, I am now stuck deleting the attribute. I see in inspect mode that the attribute is targeted, but it is not deleted.

        $('.filters-button').on('click', function() {
      // Save Parent
      let filterParent = $(this).closest('.filters__cl_item');

      // Save Parent's Index
      let filterIndex = filterParent.index();

      // Toggle Attribute in parent's Element
      if (filterParent.attr('filter')) {
            filterParent.removeAttr('filter');
        } else {
            filterParent.attr('filter', 'selected');
        }

      // Toggle Cookies
      if (filterParent.attr('filter', 'selected')) {
        let cookieName = "item_" + filterIndex;
        Cookies.set(cookieName, "selected", { expires: 60 });
      } else {
        let cookieName = "item_" + filterIndex;
        Cookies.remove(cookieName);
      }
    });



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source