0

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.

2
  • You can probably solve this on your own using DevTools and stepping though the code. Commented Mar 27, 2017 at 9:41
  • You have to initialise a property before setting its property. Try others[i] = others[i] || {} Commented Mar 27, 2017 at 9:41

4 Answers 4

2

You need to create object, then set its property

others[i] = {}

$(".drinks input").change(function() {
  var others = [];
  $('.drinks input:checked').each(function(i) {
    others[i] = others[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>


You can also use .map()

$(".drinks input").change(function() {
  var others = $('.drinks input:checked').map(function(i) {
    return {
      'id': $(this).val(),
      quantity: 1
    }
  }).get();
  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>

Sign up to request clarification or add additional context in comments.

1 Comment

I'd suggest others[i] = others[i] || {}
2

The others array only has one object and therefore it will fail when you have multiple checked inputs.

Rather than initialising with one object, you can add an object to the array with each iteration.

  var others = [];
  $('.drinks input:checked').each(function (i) {
       others.push({
          id: $(this).val(),
          quantity: 1
       });
  });
  console.log(others);

Full solution:

$( ".drinks input" ).change(function() {
   var others = [];
  $('.drinks input:checked').each(function (i) {
       others.push({
          id: $(this).val(),
          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>

Comments

1

Try the following:

$( ".drinks input" ).change(function() {
  var others = [];
  $('.drinks input:checked').each(function (i) {
       others[i] = others[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>

Comments

0

minimum Line of code and working file.

    $( ".drinks input" ).change(function() {
        var others = [{}];
        $('.drinks input:checked').each(function (i) {
             others[i] = {'id': $(this).val(),'quantity' : '1'};
        });
        console.log(others);
    });

Demo:- https://jsfiddle.net/5dd4p74t/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.