views:

38

answers:

2

I am new to javascript/jquery so this is a noob question :)

I am trying to get stuff into table with jquery. The stuff/data is in attributes and array and I would need to go trough that array to get that stuff.

In this I put some content into a array:

$(Stuff).find("desc").each(function(index) {
stuffArray[index]=$(this).text();
});

Here I put it into a table

$("#table").append('<td><a href="'+ siteRoot+'/'+item.url'"></a><td>'+item.title'</td><td>' + stuffArray[i+1] + '</td>');

I should loop the stuffArrau there I guess? But I dont know how to do it inse append thing like that.

A: 

If you want to loop round the stuffArray and add the contents to the table you can do something like:

for(var x=0;x<stuffArray.length;x++){
    $("#table").append('<tr><td>'+stuffArray[x]+'<td></td>'+stuffArray[x]+'</td></tr>');
}

see a stripped down example here

Update In response to your comments: You still want to loop round but rather than create a new row for each element in StuffArray, add a new <td> element to the table, like:

var markup = '<tr>';
for(var x=0;x<stuffArray.length;x++){
  markup += '<td>'+stuffArray[x]+'</td>';
}
markup+='</tr>';
$('#table').append(markup);

Updated Example

Fermin
Problem with this is that some of the thing going into table do not come from that stuffArray. And I do like that, then some of the things will come multiple time (the things that dont come from array).
ogk
Sorry, I've no idea what you mean. Where do the other things come from, what do you want from stuffArray? What do you want the final table to contain?
Fermin
Basically this: <tr><td>Link</td><td>title</td><td>stuffArray1</td><td>stuffArray2</td><td>StuffArrayX</td></tr> and so on as much as there is stuff in stuffArray
ogk
The link and title comes from different attribute; item.link and item.title.
ogk
Updated answer.
Fermin
A: 

you should probably enclose the table construction in a for loop then. The structure is like:

 for(var i = 0; i < stuffArray.length; i++) {
    $("#table").append('<tr><td><a href="'+ siteRoot+'/'+item.url'"><td>'+item.title'</td><td>' + stuffArray[i+1] + '</td></tr>');

}
arakno