How do I use array.push to add an object with a custom index in an array?
I have this script but it doesnt work. Chrome’s Javascript console is not outputting errors
var tempList = new Array();
$('#add').click(function(){
var split = $('#itemid').val().split('_');
var itemid = split['0'];
var lbs = $('#lbs').val();
tempList.push(tempList['itemid'] = lbs);
for (var i; i<=(tempList.length - 1); i++){
if (i==0){
var list = tempList[i]+ '<br />';
}
else{
var list = list + tempList[i]+ '<br />';
}
}
alert(list);
});
Old answer: Your script may not work, because you have to initialise the index of the loop:
var i=0;Question (new, at the comment chain):
Answer:
See the code below. After the loop, I have included several ways to append the data/table to the body, because you didn’t specify which method you wanted.