Jenna
Jenna

Reputation: 25

Looping through multiple checkboxes and adding the checked values to an array to write back to Sharepoint

I have a block of code that is supposed to look at which checkboxes are checked and then write the value of those checkboxes to an array so that I can write the data back to Sharepoint. I got it working before when I used an if/else statement, but I need it to be able to handle multiple checkboxes being selected. I did some research and found part of my code from another answer and combined it with some of my code but it doesn't seem to be working and I'm not sure why. None of the other answers I came across seem to be what I'm looking for.

Basically, what I'm asking is, what is the best way to achieve what I'm trying to do?

function addCustomers() {
    $(document).ready(function () {
        var customers = [];
        var ckbox = $('.customer-options');

        $('input').on('click', function () {
            if (ckbox.is(':checked')) {
                customers.push(ckbox).val();
            }
            else {
            }
        });

        customers.toString();
        document.getElementById('mxID04').innerHTML = customers;
    });
}

Upvotes: 1

Views: 296

Answers (1)

ControlAltDel
ControlAltDel

Reputation: 35106

It's not exactly clear what you are trying to do. First, your function contains document.ready which doesn't look right. Then you create some vars which I think you want to fill because at the end you are setting another element's innerHTML to, but in between you've just created a click handler which isn't going to work. Here's what I've put together for testing. I think you should be able to modify my example into what it is you're after

function addCustomers() { 
  var customers = []; 
  var ckbox = $('.customer-options'); 
  alert('add handler');
  $('input').change(function() {
    $('input:checked').each(function() {
      customers.push(this);
    });
  });
}


$( document ).ready(function() {
//    alert('here');
    addCustomers();
});

Upvotes: 1

Related Questions