0

I am looking to loop through items in a select box, to see if any of them already have quantity saved in the API, and append said item to the page.

My code does this with the use of .trigger('change'), however it then exit the for loop.

Without .trigger('change'), the for loop itself will complete, however without the .trigger('change'), no item is appended.

$(window).on('load', function(e) {
    if (Basket) {
        var lineItemArray = [11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 4];
        for (i=0; i < lineItemArray.length; i++) {
            if (Basket.LineItems[lineItemArray[i]].Quantity > 0) {
                // Append item to page (function elsewhere takes care of this)
                $('#select-other-items-section').find('option[value="' + lineItemArray[i] + '"]').attr("selected",true).trigger('change');
                }
            }
        }
    });

Is anyone kindly able to advise what is wrong? Or perhaps it is not even possible to use .trigger within a loop?

6
  • 3
    An option doesn't change, the select does Commented Jul 12, 2017 at 15:20
  • I think you'd be better off using something other than a "change" event to trigger appending your items -- why not just call the append function directly? Commented Jul 12, 2017 at 15:30
  • Does the 'change' event reload the page? Commented Jul 12, 2017 at 15:31
  • Update your 'change' handler so that it does a console.log('change');return; (ie nothing else) and re-run your code. If it then works then it's something in the change handler itself which you've not included here. Commented Jul 12, 2017 at 15:33
  • 1
    Your i variable is global. Change for (i=0; to for (var i=0;. (or use something other than i, eg j or ii). If your change event (or some other code, such as jquery internal code) also has a for (i=0.. loop (or any code than affects i) then it will reset the i and break this loop. Commented Jul 12, 2017 at 15:36

2 Answers 2

1

In this line:

for (i=0; i < lineItemArray.length; i++)

the i variable becomes global. If the global i is set anywhere else while inside the loop (in this case within the change event handler) then it will also change the i used here.

Fix by changing i to the current scope:

for (var i=0; i < lineItemArray.length; i++)
Sign up to request clarification or add additional context in comments.

Comments

1

You can pass a filtered array directly to a multiple <select> using val(). Then trigger the change on the <select>

var filteredVals = lineItemArray.filter(function(val, i){
    if(!Basket.LineItems[val].Quantity){
          return false; // don't want this one
     }
     // Append item to page here
     return true;
});

$('#select-other-items-section').val(filteredVals).change();

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.