0

Desired Functionality: On selecting a checkbox, a span is created with an id & data attribute same as the checkbox and appended to a div. On clicking the 'x' on this span should uncheck the checkbox and remove the span as well.

Issue: On selecting the checkbox, an additional span with an 'undefined' label is created.enter image description here

JSFIDDLE

  var filtersApplied = [];
  $('.ps-sidebar').on('change', 'input[type=checkbox]', function () {
  var me = $(this);
  console.log('me', me);
  if (me.prop('checked') === true) {
    filtersApplied.push([
      ...filtersApplied,
      { id: me.attr('id'), data: me.attr('data-filter-label') }
    ]);
  } else {
    filtersApplied = filtersApplied.map(function (item, index) {
      return item.filter(function (i) {
        return i.id !== item[index].id;
      });
    });
  }

  if (filtersApplied.length === 0) {
    $('.ps-plans__filters').hide();
    $('.ps-plans__filters-applied').html('');
  } else {
    $('.ps-plans__filters').show();
    var filtersAppliedHtml = '';
    filtersApplied.map(function (elements) {
      console.log('items', elements);
      return elements.map(function (el, i) {
        console.log('item', el);
        return (filtersAppliedHtml +=
          '<span class="ps-plans__filter" id="' + el.id + '_' + i +'">' +el.data +
          '<span class="icon-remove-circle remove-filter" data-filter="' +el.data +'"> X</span></span>');
      });
    });
    console.log('filtersAppliedHtml', filtersAppliedHtml);
    console.log($('.ps-plans__filters-applied').html(filtersAppliedHtml));
  }
});

2 Answers 2

1

Your undefined label is because of the ...filtersApplied

  if (me.prop('checked') === true) {
    filtersApplied.push([
      //this ...filtersApplied
      { id: me.attr('id'), data: me.attr('data-filter-label') }
    ]);

Note that filtersApplied is an array and you're making a push(), this method inserts a value in the end of the array, so your ...filtersApplied makes no sense. Just remove it and you'll be fine. You can se more here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push

Sign up to request clarification or add additional context in comments.

Comments

0

there are few thing that need to be fixed.

  • when adding an element you should not push filtersApplied along with new object. instead you better do arr = [...arr, obj];
  • when remove an item you could apply a filter instead based on me.attr('id'). with map you would get undefined values;
  • after that you would map only once to build your html content, not twice;

var filtersApplied = [];
$('.ps-sidebar').on('change', 'input[type=checkbox]', function () {
  var me = $(this);
  if (me.prop('checked') === true) {
    filtersApplied = [
      ...filtersApplied,
      { id: me.attr('id'), data: me.attr('data-filter-label') }
    ];
  } else {
    filtersApplied = filtersApplied.filter(function (item, index) {
      return me.attr('id') !== item.id;
    });
  }

  if (filtersApplied.length === 0) {
    $('.ps-plans__filters').hide();
    $('.ps-plans__filters-applied').html('');
  } else {
    $('.ps-plans__filters').show();
    var filtersAppliedHtml = '';
    filtersApplied.map(function (el, i) {
        return (filtersAppliedHtml +=
          '<span class="ps-plans__filter" id="' +
          el.id +
          '_' +
          i +
          '">' +
          el.data +
          '<span class="icon-remove-circle remove-filter" data-filter="' +
          el.data +
          '">  X</span></span>');
    });
    
    $('.ps-plans__filters-applied').html(filtersAppliedHtml);
  }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.