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?
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.ivariable is global. Changefor (i=0;tofor (var i=0;. (or use something other thani, egjorii). If your change event (or some other code, such as jquery internal code) also has afor (i=0..loop (or any code than affectsi) then it will reset theiand break this loop.