I know there are a lot of these questions on Stackoverflow but none of them work for me. I would like to post my own code and try and find out what I am doing wrong.
So, I have a function that adds a bunch of items to an array. The page is never refreshed, as is the way it is designed. So when that same function is called again the array MUST be emptied because:
- Items added to an array are linked to a specific item on the page that the user selected.
- When a new item is selected the old item's things should not be shown in the array. Only the new item's things.
- The array is populated from checkbox values and that array is then used to create different checkboxes on a popup modal form.
Here is the code to populate the array:
// Here I tried emptying the array with below mentioned methods. The array is defined outside of the function. I have also tried defining it inside.
var count = 0;
var optionsSelected = 0;
$('input[name="extrasSelected"]:checked').each(function() {
var selected = $(this).val().split("|");
extras[count] = [selected[0], selected[1], selected[2], selected[3], 'checked'];
optionsSelected++;
count++;
});
$('input[name="extrasSelected"]:not(:checked)').each(function() {
var notSelected = $(this).val().split("|");
extras[count] = [notSelected[0], notSelected[1], notSelected[2], notSelected[3], 'notChecked'];
count++;
});
extras.sort();
This entire thing is working perfectly. Except when I select a new item. The popup displays and checkboxes are created with the previous item's things as well as checkboxes with the new item's things.
I have tried to use:
extras = [];
extras.pop();
extras.length = 0
while (extras > 0) {
extras.pop();
}
Actually, I have tried all methods seen on stackoverflow and google. So I am guessing that I am doing something wrong.
Any help would be greatly appreciated!
EDIT
Additional Code As Requested:
var extras = [];
$("#doLookup").click(function() {
var count = 0;
var optionsSelected = 0;
$('input[name="extrasSelected"]:checked').each(function() {
var selected = $(this).val().split("|");
extras[count] = [selected[0], selected[1], selected[2], selected[3], 'checked'];
//alert(extras[0][1]);
optionsSelected++;
count++;
});
$('input[name="extrasSelected"]:not(:checked)').each(function() {
var notSelected = $(this).val().split("|");
extras[count] = [notSelected[0], notSelected[1], notSelected[2], notSelected[3], 'notChecked'];
//alert(extras[0][1]);
count++;
});
extras.sort();
if (extras.length > 0) {
optionalsJSHead = '<table width="100%"><tr><th style="width: 40%;"><b>Optional Extra</b></th><th style="text-align:right;width:15%"><b>Retail</b></th><th style="text-align:right;width:15%"><b>Trade</b></th><th style="text-align:right;width:15%"><b>Market</b></th><th style="text-align:right"><b>Include</b></th></tr>';
optionalsJSBody = '';
if (parseInt(year) === parseInt(guideYear)) {
for (i = 0; i < extras.length; ++i) {
optionalsJSBody += '<tr><td>'+extras[i][0]+'</td><td align="right">R '+extras[i][1].replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,")+'</td><td align="right">R '+extras[i][2].replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,")+'</td><td align="right">R '+extras[i][3].replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,")+'</td><td align="right"><input class="chckDis" type="checkbox" name="extrasSelectedAdjust" id="'+ extras[i][0] +'" value="'+ extras[i][0] +'|'+ extras[i][1] +'|'+ extras[i][2] +'|'+ extras[i][3] +'" disabled="disabled"/></td></tr>';
}
} else {
for (i = 0; i < extras.length; ++i) {
if (extras[i][4] == 'notChecked') {
optionalsJSBody += '<tr><td>'+extras[i][0]+'</td><td align="right">R '+extras[i][1].replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,")+'</td><td align="right">R '+extras[i][2].replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,")+'</td><td align="right">R '+extras[i][3].replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,")+'</td><td align="right"><input class="chckDis" type="checkbox" name="extrasSelectedAdjust" id="'+ extras[i][0] +'" value="'+ extras[i][0] +'|'+ extras[i][1] +'|'+ extras[i][2] +'|'+ extras[i][3] +'"/></td></tr>';
} else {
optionalsJSBody += '<tr><td>'+extras[i][0]+'</td><td align="right">R '+extras[i][1].replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,")+'</td><td align="right">R '+extras[i][2].replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,")+'</td><td align="right">R '+extras[i][3].replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,")+'</td><td align="right"><input class="chckDis" type="checkbox" name="extrasSelectedAdjust" id="'+ extras[i][0] +'" value="'+ extras[i][0] +'|'+ extras[i][1] +'|'+ extras[i][2] +'|'+ extras[i][3] +'" checked /></td></tr>';
}
}
}
optionalFooter = '</table>';
optionalsJS = optionalsJSHead + optionalsJSBody + optionalFooter;
}
});