How to show checkbox checked in data-id attribute

Viewed 90

I found this code:

var checkboxes = $('input[type=checkbox]');
checkboxes.on('change', function() {
  $('#divfilter').text(function() {
    return checkboxes.filter(':checked').map(function() {
      return this.name;
    }).get()    
    .join(', ') + '.';
  });
});
<input type="checkbox" name="barbecue" id="barbecue" value="oui" class="barbecue" />
<input type="checkbox" name="handicap" id="handicap" value="oui" class="handicap" />
<input type="checkbox" name="animaux" id="animaux" value="oui" class="animaux" />

<div id="divfilter"></div>

It works perfectly but I need the data to be displayed here inside data-namevar="HERE":

<button
  data-namevar="I NEED CHECKBOX CHECKED SHOW HERE"
  class="add-to-cart btn_1 gradient medium full-width">Add to cart <i class="icon_bag_alt"></i>
</button>

How to do it?

2 Answers

Can you try this ?

var checkboxes = $('input[type=checkbox]');
checkboxes.on('change', function() {
  $('#divfilter').text(function() {
    return checkboxes.filter(':checked').map(function() {
      return $(this).attr('data-namevar');
    }).get()    
    .join(', ') + '.';
  });
});

And now, how do I display that data exactly in my html?

<button
    data-namevar="I NEED CHECKBOX CHECKED NAMES SHOW HERE">
    Add to cart
</button>
Related