0

In the web application I'm currently working on there are a lot of places where I have many HTML checkboxes that I need to turn into a JS array of selected items. For example:

<input type="checkbox" class="example" data-item="3">
<input type="checkbox" class="example" data-item="5">
<input type="checkbox" class="example" data-item="7">
<input type="checkbox" class="example" data-item="11">

Then I have a lot of logic like this to pack the selected values into an array:

var items = [];
$('.example[data-item]:checked').each(function (_, e) {
    items.push(Number($(e).data('item')));
});

My question is just this: Is there some nice way to do this in a one-liner (jQuery is allowed if needed), e.g.:

var items = /* magic */;

I realize I could write a function for this, and I will if there is no way to do it clearly otherwise, but I'm looking for some built-in JS magic that can do it all at once.


For completeness here's a snippet:

$('#button').click(function () {
  var items = [];
  $('.example[data-item]:checked').each(function (_, e) {
    items.push(Number($(e).data('item')));
  });
  alert(JSON.stringify(items));
});
label { display: flex; align-items: center; }
div { display: inline-flex; flex-direction: column; }
button { margin: 1ex; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<label><input type="checkbox" class="example" data-item="3"> Item 3</label>
<label><input type="checkbox" class="example" data-item="5"> Item 5</label>
<label><input type="checkbox" class="example" data-item="7"> Item 7</label>
<label><input type="checkbox" class="example" data-item="11"> Item 11</label>
<label><input type="checkbox" class="example" data-item="13"> Item 13</label>
<label><input type="checkbox" class="example" data-item="17"> Item 17</label>
<label><input type="checkbox" class="example" data-item="19"> Item 19</label>
<button id="button">Click Me</button>
</div>
Jason C
  • 38,729
  • 14
  • 126
  • 182

3 Answers3

2

I don't see anything wrong with your code but another way could be to use map() and return what you need:

$('#button').click(function() {
  var items = $(".example[data-item]:checked").map(function() {
    return $(this).data('item');
  }).get();
  console.log(items);
});
label {
  display: flex;
  align-items: center;
}

div {
  display: inline-flex;
  flex-direction: column;
}

button {
  margin: 1ex;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <label><input type="checkbox" class="example" data-item="3"> Item 3</label>
  <label><input type="checkbox" class="example" data-item="5"> Item 5</label>
  <label><input type="checkbox" class="example" data-item="7"> Item 7</label>
  <label><input type="checkbox" class="example" data-item="11"> Item 11</label>
  <label><input type="checkbox" class="example" data-item="13"> Item 13</label>
  <label><input type="checkbox" class="example" data-item="17"> Item 17</label>
  <label><input type="checkbox" class="example" data-item="19"> Item 19</label>
  <button id="button">Click Me</button>
</div>
Ionut Necula
  • 11,107
  • 4
  • 45
  • 69
  • 1
    Awesome, and I made it even shorter with `var items = $.map($('.example[data-item]:checked'), (e) => +e.dataset.item);` (Just discovered the `+` trick from [jquery multiple checkboxes array](https://stackoverflow.com/a/6166791/616460)). – Jason C Jul 17 '17 at 13:55
1

Yes you can use map function:

var tempValue=$('input:checkbox').map(function(n){
          if(this.checked){
                return  this.value;
              };
       }).get().join(',');
   console.log(tempValue);
Himanshu Upadhyay
  • 6,558
  • 1
  • 20
  • 33
1
const checkedItems = Array.from(document.querySelectorAll('[checked]')).reduce((items, el) => {
   // save whatever you want from the elmenent here
   items = items.concat(el)
   return items;
}, [])
Avraam Mavridis
  • 8,698
  • 19
  • 79
  • 133