lundi 23 mai 2016

javascript/jquery: how to add/remove variables from array depending on whether a checkbox is selected

I'm outputting order addresses for a takeout restaurant: each individual order is output as a table, each table has a checkbox. I want to put the addresses into an array when the .ordercollected checkbox is ticked, and remove it from the array if it is unticked.

At the moment, rather than appending each new address I get each order address on its own in the array, which updates each time I tick the .ordercollected checkbox.

Really new to programming so any help appreciated!

//get the addresses from selected tables
$('.ordercollected').change(function() {
    var activeaddress = [];
    //loop through checkboxes with class .ordercollected
    $(this).each(function() {
        //if checkbox is ticked
        if ($(this).is(':checked')) {
            //get address from table
            var address = $(this).closest('.ordertable').find('.address').text();
            //append value of address into activeaddress array
            activeaddress.push(address);
        };
    });
    console.log('active address: ', activeaddress);
});




Aucun commentaire:

Enregistrer un commentaire