I was wondering, is it possible to run a for loop inside of a variable? I have a variable defined as var table = $('<table border="1"><tr></tr>'); I would like to populate that first row with <th>s generated from an array. In theory, it would look something like:
var table = $('<table border="1"><tr>'for (var i=0; i< headers.length; i++){$("<th>"+headers[i]+"</th>")}'</tr>');
I have tried to run the for loop as
for (var i=0; i< headers.length; i++) {
$("<th>"+headers[i]+"</th>").appendTo(table);
}
outside of the variable declaration, but because of other parts of the program, using .appendTo is not a viable solution.
If you really can’t do it outside of the variable declaration (which I don’t see why you couldn’t), how about something like this:
Working example here: http://jsfiddle.net/hEKy5/