I want to create dynamic table row from a string variable like below:
var c = apple,mango,jackfruit,guava,orange;
var arr = c.split(',');
var trval='<tr>';
$.each(arr,function(index,value){
trval = trval+'<td>'+value+'</td>';
});
trval = trval+'</tr>';
The above example works well and creates following table row:
<tr>
<td>apple</td>
<td>mango</td>
<td>jackfruit</td>
<td>guava</td>
<td>orange</td>
</tr>
But if any table cell needs a special mark-up such as needs to be red then the table row should be:
<tr>
<td>apple</td>
<td style="color:red">mango</td>
<td>jackfruit</td>
<td>guava</td>
<td>orange</td>
</tr>
Then the array should be:
var c = apple,["style='color:red'","mango"],jackfruit,guava,orange;
Here the style='color:red'
is optional. i.e it may be or may not be there.
So, I have to iterate the var c
in such way that it will search if individual value in arr
is a variable. If it is variable, then it will iterate that array (in this case, ["style='color:red'","mango"]
to create that table cell and its optional mark-up. This optional array in the string is dynamic and it could be for any element of the string or even none for those element.
How to make the jquery.each code in that case?