Declare empty array in javascript
In the succes of your ajax call use the function push()
product.push([]);
This adds an array at the last index of product
. Like that ,the index 10
is created and you have an extra data.
If you want to add a dynamic number of rows, use this code :
var number_of_new_row = 11; // 11 for example, you can use any number >= 0
for(; number_of_new_row--;)
product.push([]);
Another way
In your ajax return save the new length of your array product
in a global variable.
And use it before your loop to reset your array and initialize it with the new length.
var lengthArray = 10; // update the value in the callback of your ajax call
And your loop :
var product = [];
for(; lengthArray--;)
product.push([]);
$('#contextreload ul').each(function(i, ul) {
//<strike> var product = $(ul).html(); </strike>
allline = i;
$('#reloadajax'+i+' li').each(function(lk, li) {
var lilk = $(li).html();
product[i][lk]=lilk;
alert(lilk+lk);
// your code goes here
});
// your code goes here
});
Note: this line of your code produces a string, not an array.
var product = $(ul).html(); //returns string not an array
what you need is something like
var product_arr = {}; // an object or
var product_arr = []; // an array
The following code used to declare empty array in javascript
var product_arr = new Array(); //declaring empty array
console.log(product_arr);