JavaScript Adding a for loop inside jQuery append method

I am having a table into which most elements are static.I however I have one <tr></tr> that i want to change dynamically using a for loop.I have only been able to add a for loop that updates everything in the table.

for(var x = 0; x< foo.length; x++){//Move the for loop from here
$("div#show_details").append('<table class="'+receipt_no+'" id="print_me" style="background:#ffffff;width:800px;font-family:Roboto;text-align:center;">',
    '<tbody>',
    '<tr><td colspan="3"><h4 style="font-size:20px;color:#009688;text-align:center">Thank you Student Name,</h4></td></tr>',
    '<tr><td colspan="3">Your order for receipt no: <b>'+receipt_no+'</b> has the following items.</td></tr>',
    '<tr><td td colspan="3"></td></tr>',
    '<tr><td>#</td><td style="text-align:left">Item</td><td>Price</td></tr>',
    //I want to use a for loop inside here

    '<tr><td>'+foo[x].foodCount+'</td><td style="text-align:left">'+foo[x].foodName+'</td><td>'+foo[x].price+'</td></tr>',
    '<tr style="font-weight:bold"><td colspan="2" style="text-align:left">Total</td><td>'+foo[0].price*foo[0].foodCount+'</td></tr>',
    '</tbody>',
    '</table>',
    '<a class="btn btn-xs btn-info" target='_blank' href="javascript:void(processPrint());">Print</a>'
    );
    }

I am having trouble adding the for loop inside of the append method to ensure that only contents of <tr><td>'+foo[x].foodCount+'</td><td style="text-align:left">'+foo[x].foodName+'</td><td>'+foo[x].price+'</td></tr>', are changed. Any help will be appreciated.

Answer:1

you can't put a loop in there, you'll need to rethink how you do that - e.g. create a string with the "dynamic" content using a loop, then use that string as part of the appended content, something like

var dynamic = "";
for (var x = 0; x < foo.length; x++) { //Move the for loop from here
    dynamic += '<tr><td>' + foo[x].foodCount + '</td><td style="text-align:left">' + foo[x].foodName + '</td><td>' + foo[x].price + '</td></tr>';
};
$("div#show_details").append('<table class="' + receipt_no + '" id="print_me" style="background:#ffffff;width:800px;font-family:Roboto;text-align:center;">',
    '<tbody>',
    '<tr><td colspan="3"><h4 style="font-size:20px;color:#009688;text-align:center">Thank you Student Name,</h4></td></tr>',
    '<tr><td colspan="3">Your order for receipt no: <b>' + receipt_no + '</b> has the following items.</td></tr>',
    '<tr><td td colspan="3"></td></tr>',
    '<tr><td>#</td><td style="text-align:left">Item</td><td>Price</td></tr>',
    dynamic,
    '<tr style="font-weight:bold"><td colspan="2" style="text-align:left">Total</td><td>' + foo[0].price * foo[0].foodCount + '</td></tr>',
    '</tbody>',
    '</table>',
    '<a class="btn btn-xs btn-info" target='_blank' href="javascript:void(processPrint());">Print</a>'
);
Answer:2

I am using deckgrid in conjunction with a custom built infinite scroller to load a continuous stream of photos in Angular laid out in a masonry layout. The data set returned each time is quite large ...

I am using deckgrid in conjunction with a custom built infinite scroller to load a continuous stream of photos in Angular laid out in a masonry layout. The data set returned each time is quite large ...

  1. angular infinite scroll example
  2. angular infinite scroll table
  3. angular infinite scroll grid
  4. angular infinite scroll component
  5. angular infinite scroll list
  6. angular infinite scroll directive
  7. angular infinite scroll material
  8. angular infinite scroll select
  9. angular infinite scroll mat table
  10. angular infinite scroll cards
  11. angular infinite scroll cdk
  12. angular infinite scroll throttle
  13. angular infinite scroll
  14. angular infinite scroll not working
  15. angular infinite scroll observable
  16. angular infinite scroll http request
  17. angular infinite scroll up and down
  18. angular infinite scroll horizontal
  19. angular infinite scroll up
  20. angular infinite scroll performance

I am try to install React native with React Stable version but it install React: 16-alpha "dependencies": { "react": "16.0.0-alpha.6", "react-native": "0.44.0" } So How Can i install React-...

I am try to install React native with React Stable version but it install React: 16-alpha "dependencies": { "react": "16.0.0-alpha.6", "react-native": "0.44.0" } So How Can i install React-...

  1. install react native cli
  2. install react native mac
  3. install react native windows
  4. install react native ubuntu
  5. install react native npm
  6. install react native app
  7. install react native elements
  8. install react native firebase
  9. install react native navigation
  10. install react native app on android
  11. install react native debugger
  12. install react native vector icons
  13. install react native cli globally
  14. install react native windows 10
  15. install react native expo
  16. install react native paper
  17. install react native app on iphone
  18. install react native screens
  19. install react native visual studio code
  20. install react native ios

I am working on a project where I want to display an image and when the mouse moves over the image, the image disappears and info on the page appears. When the mouse moves out of the element, the ...

I am working on a project where I want to display an image and when the mouse moves over the image, the image disappears and info on the page appears. When the mouse moves out of the element, the ...

I am pulling some text from a database and I have , and I would like to replace them with <br> don't really know why it's not working any ideas? JS Code $( document ).ready(function() { //....

I am pulling some text from a database and I have , and I would like to replace them with <br> don't really know why it's not working any ideas? JS Code $( document ).ready(function() { //....