J
J
jakiro01022021-07-12 14:25:18
JavaScript
jakiro0102, 2021-07-12 14:25:18

How to add and remove identical values ​​(text) in Jquery to a block?

There is a filter with checkboxes (see the picture), I'm trying to filter
60ec263ddbb13295952730.png
When any checkbox is selected, the value from the selected checkbox should be written to the top field, with several options, all the options that were selected should be written to the field

When the checkbox is removed, the field should disappear too selected answer. How to do it?
I tried to do it all through an array, but I got to the point of checking and deleting the same values, and blunt. How can this be resolved?

<div class="form-filter press-center__form-type js-filter js-open-filter-block filter-topic-js"
     id="press-center-filter-theme">
    <span class="js-type-result" data-title="Тема">Тема</span>
    <div class="form-hide js-hide">
        <div class="dfth__item js-select-all">
            <input type="checkbox" class="dfth__check" id="dfth-check-0">
            <label for="dfth-check-0" class="dfth__label">Все темы</label>
        </div>
        <div class="dfth__item js-select-item">
            <input type="checkbox" class="dfth__check" id="dfth-check-231" data-id="231">
            <label for="dfth-check-231" class="dfth__label">Новости</label>
        </div>
        <div class="dfth__item js-select-item">
            <input type="checkbox" class="dfth__check" id="dfth-check-232" data-id="232">
            <label for="dfth-check-232" class="dfth__label">Сбыт продукции</label>
        </div>
        <div class="dfth__reset">Очистить всё</div>
    </div>
</div>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2021-07-12
@jakiro0102

$('.js-filter').change(function() {
  const selected = $('.dfth__check:checked', this)
    .closest('.dfth__item')
    .find('.dfth__label')
    .get()
    .map(n => n.innerText)
    .join(', ');

  $('.js-type-result', this).text(selected || дефолтное_значение);
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question