0

if I click on one checkbox, need to create a dynamic array based on its data-attribute value and push all checked checkbox value with same data-attribute value to this array.

Ex: if i click on height,i need to create an array with its data-attribute value and push all checkbox with checked value in this array

<input type="checkbox" name="" class="checkbox" data-parent="1" value="1">brand
<input type="checkbox" name="" class="checkbox" data-parent="2" value="2">style
<input type="checkbox" name="" class="checkbox" data-parent="3" value="3">height
<input type="checkbox" name="" class="checkbox" data-parent="4" value="4">width
<input type="checkbox" name="" class="checkbox" data-parent="2" value="5">style
<input type="checkbox" name="" class="checkbox" data-parent="3" value="6">height
<input type="checkbox" name="" class="checkbox" data-parent="3" value="7">height
    <script type="text/javascript">
        $('.checkbox').click(function(){
            var newarray = [];
            var id = $(this).val();
            var pid = $(this).attr('data-parent');
            newarray[pid] = id;
        });

    </script>
13
  • So, this question was already asked, and answered. Why was it deleted? The answer is still valid Commented Aug 2, 2019 at 16:41
  • @Taplar you are just pushing the attribute value to a array, here i need to create an array for each like if i click height,create one array for height and push all checked height value to this array Commented Aug 2, 2019 at 16:56
  • Then you need to include the html associated with what you are trying to do to make it more clear. Commented Aug 2, 2019 at 16:58
  • @Taplar updated the question Commented Aug 2, 2019 at 17:00
  • So, if one height is checked, you want the array to contain all the values for all the heights, regardless of if they are checked or not? Commented Aug 2, 2019 at 17:02

1 Answer 1

1

var filterCriteria = {};

var $checkboxes = $('.checkbox').click(function() {
  var $this = $(this);
  var parentValue = $this.data('parent');
  
  filterCriteria[parentValue] = $checkboxes
    .filter(`[data-parent="${parentValue}"]:checked`)
    .map(function(){ return this.value })
    .get();
    
  console.log( filterCriteria );
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" name="" class="checkbox" data-parent="1" value="1">brand
<input type="checkbox" name="" class="checkbox" data-parent="2" value="2">style
<input type="checkbox" name="" class="checkbox" data-parent="3" value="3">height
<input type="checkbox" name="" class="checkbox" data-parent="4" value="4">width
<input type="checkbox" name="" class="checkbox" data-parent="2" value="5">style
<input type="checkbox" name="" class="checkbox" data-parent="3" value="6">height
<input type="checkbox" name="" class="checkbox" data-parent="3" value="7">height

This may be more what you're after. If you are wanting to group values by their parent key, to know which values to filter on which key. It filters the checkboxes by the checkbox changed, maps out their values, gets the basic array, and then sets them on the filter object.

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

3 Comments

you are just pushing the attribute value to a array, here i need to create an array for each like if i click height,create one array for height and push all checked height value to this array
if unchecking,how can we remove this value
It's already removing it. The parent will still be there, but with an empty array

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.