简体   繁体   中英

How to show checkbox checked in data-id attribute

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?

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM