Helo, i'm trying save to array or object data with checked checkboxes, but still it return me error. I don't know why, because i defined a variable before a each.
My code is here:
$( ".drinks input" ).change(function() {
var others = [{}];
$('.drinks input:checked').each(function (i) {
others[i]['id'] = $(this).val();
others[i].quantity = 1;
});
console.log(others);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="drinks">
<input type="checkbox" value="1">
<input type="checkbox" value="2">
<input type="checkbox" value="3">
</div>
Can you help me plese how can I right define a array? Thank you so much.
others[i] = others[i] || {}