-2

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);
});

edit to add in the tables I am creating:

   <table class="ordertable">
      <tr>
        <td>
          <p>Order #
            <?php echo $order_id; ?> &mdash;
              <time datetime="<?php the_time('c'); ?>">
                <?php echo the_time('d/m/Y g:i:s A'); ?>
              </time>
          </p>
        </td>          
        <td>
         <?php echo $order->billing_first_name . ' ' . $order->billing_last_name ?>
        </td>
        <td>
          <?php if ($order->billing_phone) : ?>
            <a href="tel:<?php echo $order->billing_phone; ?>">Tel.</a>
            <?php endif; ?>
        </td>
        <td>
        <p class="address"><?php echo $order->shipping_address_1 . ' ' . $order->shipping_postcode ?></p>
        <td/>
        <td>
        <a class="maps-activate" href="#">Open in Maps</a> 
        </td>
        <td>
          <form action="">
            <input type="checkbox" class="ordercollected" value="0" />
          </form>
        </td>
      </tr>
    </table>
Theo
  • 495
  • 4
  • 12

2 Answers2

1

Rather than remake your entire activeaddress array every time a checkbox changes, the best thing to do here would be to add or remove only the selected address when a checkbox changes. To do this activeaddress will have to be available outside of that function. I also think it will be cleaner if you use a JS object instead of an array.

var activeaddress = {};

$('.ordercollected').change(function() {
    // get table id
    var orderTableID = $(this).closest('.ordertable').attr('id');
    // 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 object
        activeaddress[orderTableID] = address;
    } else {  // checkbox is NOT ticked
        // remove address from object
        delete activeaddress[orderTableID];
    }
    console.log("active address: ", activeaddress); 
});

As you can see, this code assumes that each table with class .ordertable has a unique id that can be used as the key in the activeaddress object. This is better than looping over the entire array/object each time because, especially if you have a very big set of orders. If you had included your HTML I would be able to help more, but as the question is this is as far as I can help. Let me know if you have any follow up questions.

A couple of things to note:

  • Using pascalCase for variable names and class names makes code more readable (e.g. activeAddress instead of activeaddress)
  • In my opinion, using an object instead of an array is a better way to add and remove a specific item
  • When asking question on SO, please give as much information as possible, such as including your HTML

Finally some links:

Community
  • 1
  • 1
Danny Harding
  • 1,666
  • 19
  • 25
  • Hi Danny, thanks very much for this. The addresses still appear to be only individually showing up in the object, rather than adding each address with a tick in the table to an object. The reason I am trying to create an array is so I can pass it to google directions api as the waypoints to a journey. I've added in the html which is in a while php loop and is created for each existing order. – Theo May 23 '16 at 15:53
  • @Kevin. that is very true. deleted previous comment based on the correct info – Julian May 23 '16 at 15:55
  • @Theo make sure you actually give each table an id attribute: ``. Looks like you could use the `$order_id` as the id for the table. Also, because you need an array for the Google API, this may not be the best solution for you. When I wrote the answer, needing to use an array was not detailed in the question.
    – Danny Harding May 23 '16 at 15:58
  • @DannyHarding You where right, I used the $order_id as an id and its now working! Yes google directions api takes the waypoints as an array, apologies for not stating the array was necessary. – Theo May 23 '16 at 16:08
  • @DannyHarding as it makes more sense to use an object - would turning the object into an array to use with google maps once the orders are selected be an ok way to go? http://stackoverflow.com/questions/6857468/a-better-way-to-convert-js-object-to-array – Theo May 23 '16 at 16:11
  • I think that would be a good way to go, especially because according to that link you shared it's pretty easy to turn a JS object into an array. – Danny Harding May 23 '16 at 16:19
  • 1
    @DannyHarding yep its working no problem, thanks very much for this - i've been banging my head against a wall all day. – Theo May 23 '16 at 16:20
0

try something like this?

HTML:

<input type="checkbox" class="ordercollected" value="apple" />
<input type="checkbox" class="ordercollected" value="mango" />

JS

    $('.ordercollected').change(function() {
    var activeaddress = [];
    //loop through checkboxes with class .ordercollected
    if (this.checked) {
        activeaddress.push(this.value);            
    }
    else {
        var index = activeaddress.indexOf(this.value);
        if (index > -1) {
            activeaddress.splice(index, 1);
        }
    }
    console.log('active address: ', activeaddress);
});
Julian
  • 781
  • 1
  • 11
  • 32
  • Hi Julian, thanks for this, the tables are created in a php loop so I'm unable to change the value of the input (as far as I'm aware) – Theo May 23 '16 at 15:56