0

Using the method below I am trying to populate an existing table with data however the function fills it with the same values. I can perform such action adding append method, but in my case the table should be exist already :)

HTML

<table class="table">
        <tr><td></td></tr>
        <tr><td></td></tr>
        <tr><td></td></tr>
</table>

JQuery

$.each(data, function(i, value){
    $(".table td").text(value.product);
}); 

var data= [
{"product":"RD0"},
{"product":"RD1-184"},
{"product":"RD1-185"}
]
1
  • 1
    Hint: You're looping through the data and adding every item to each cell. You have forgotten to loop through the cells. Commented Feb 4, 2016 at 21:31

2 Answers 2

4

Here's a code snipped using your sample from above with a demo in JSFiddle.

$(function() {
  var data = [{
    "product": "RD0"
  }, {
    "product": "RD1-184"
  }, {
    "product": "RD1-185"
  }];
  var table = $('.table');
  $.each(data, function(i, value) {
    table.find('tr').eq(i).find('td').text(value.product);
  });
});

https://jsfiddle.net/qur62os2/

Sign up to request clarification or add additional context in comments.

2 Comments

Not a problem! If this solved the problem for you, would you kindly consider marking it as the answer?
Sure, I just need to wait 3 more minutes to select answer
2

you probably need something like this:

$(".table").find('td').each(function(i) {
  $(this).text(data[i].product);
});

https://jsfiddle.net/ahmadabdul3/fn8q8e3x/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.