When clicking on checkboxes I'm trying to get id of that checkbox and put it in array so I can properly show it in data-id attribute. So once i click on checkbox that id should be added or removed in data-id attribute of div with class block Here is the jsfiddle https://jsfiddle.net/chille1987/5g28uoqk/25/
<input type="checkbox" id="121asa31" class="user">
<input type="checkbox" id="121fdfd31" class="user">
<input type="checkbox" id="1213fd1" class="user">
<input type="checkbox" id="121jh31" class="user">
<div class="block" data-id="">
Some Content
</div>
$('.user').on('click', function() {
id = $(this).attr('id');
users = $('.block').data('data-id')
if($(this).is(':checked')) {
users.push(id);
users.toString();
$('.block').attr('data-id', users);
} else {
index = users.indexOf(id);
users.splice(index, 1);
$('.block').attr('data-id', users);
}
});
Expected results should look like
<div class="block" data-id="121asa31, 121jh31"></div>