0

I'm new to javascript. Please help me out with the below question.

I have a table defined.

<div id="SampleDiv">
   <table id="SampleTable">
     <tr>
       <td></td>       
     </tr>
</table>
</div>

I have an dynamic array defined var SampleArr = ["Hello","Welcome","Folks",....];

Question : How do i loop this array and assign values to the td's in the table?

desired output::

<div id="SampleDiv">
   <table id="SampleTable">
     <tr>
       <td>Hello</td> 
       <td>Welcome</td>   
       <td>Folks</td>
       ....         
     </tr>
</table>
</div>
4
  • 2
    Using jQuery: $.each( SampleArr, function(){}), using vanilla JS: SampleArr.forEach(function(){}). Commented Oct 14, 2015 at 18:56
  • 1
    Can you give me an example Commented Oct 14, 2015 at 18:58
  • Do you need this to be done in jQuery? Commented Oct 14, 2015 at 18:59
  • @user724747 I have provided some pointers, show some effort first. Commented Oct 14, 2015 at 19:02

6 Answers 6

1

jQuery solution:

  $.each(SampleArr, function(index, value) {

    $('#SampleTable td:last').after('<td>'+value+'</td>');

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

1 Comment

Hi tried this ..consider i have table like this <div id="SampleDiv"> <table id="SampleTable"> <tr> <td><label>First Name:</label></td> </tr> <tr> <td><label>Middle Name:</label></td> </tr> <tr> <td><label>Last Name:</label></td> </tr> </table> </div> and array as ["Fname","Mname","Lname"]; ..I get output as this jsfiddle.net/chandrapraba/Lxbjqyed/5
1

I have tried using jQuery. I hope it may help you.

HTML:-

<table id="mytable">
  <tr><td><label>First Name : </label></td></tr>
  <tr><td><label>Middle Name : </label></td></tr>
  <tr><td><label>Last Name : </label></td></tr>
</table>

-----------

JQuery:-

var data = ["Krishna","Kant","Shastri"];  //first name, midle name, last name

//get all <td> element
var myTD = $('#mytable td');

//iterate all <td> and append values from data array
$.each(myTD,function(idx,tdHTML){
  $(tdHTML).append(data[idx]);
});

----------
Output:-
First Name : Krishna
Middle Name : Kant
Last Name : Shastri

make sure data array should have details in order first name, middle name & last name. You can also store both label and values in same array and populate.

Thanks

If you dont know how many data is there in array than try storing label and value in json and populate like this:-

HTML:-

<table id="mytable">
</table>

jquery:-

var data = {"First Name":"Krishna", "Middle Name":"Kant", "Last Name":"Shastri", "Place":"India"};

var tableHTML = '';

$.each(data,function(key,value){
  tableHTML += '<tr><td><label>'+ key +' : '+ value +'</label></td></tr>';
});
$('#mytable').html(tableHTML);

Thanks

Comments

0

Here is some vanilla javascript to use:

var SampleArr = ["Hello","Welcome","Folks"];

var html = '<div id="SampleDiv"><table id="SampleTable"><tr>';

for (var i=0,max=SampleArr.length;i<max;i++) {
	html += '<td>'+SampleArr[i]+'</td>';  
}
html += '</tr></table></div>';
document.write(html);

Comments

0

Here man:

var template = null;
var array = ['leo', 'Liz', 'Michael'];

for(var i =0; i<array.length; i++){
    template = '<tr><td>' + array[i] + '</td></tr>';
    $('table').append(template);
}

here is a functional example: http://jsfiddle.net/leojavier/gfduxa42/

1 Comment

This doesn't match the desired output ;)
0

You can utilize the JavaScripts forEach() loop to keep this in plain JS.

var sampleArr = ["Hello,", "Welcome", "to", "the", "jungle"];

var tableRow = document.getElementById("row1");

sampleArr.forEach(function(value){
  tableRow.innerHTML += "<td>" + value + "</td>";
});
<table id="t-table">
  <tbody>
    <tr id="row1"></tr>
  </tbody>
</table>

Comments

0

And the solution Pure Javascript and DOM manipulation (without 5Go framework):

var SampleArr = ["Hello","Welcome","Folks"];
var table = document.getElementById('SampleTable');

SampleArr.forEach(function(val  , i){

  var curCell = table.rows[0].cells[i];
  if(!curCell) curCell = table.rows[0].insertCell();

   var curValue = curCell.innerText || curCell.textContent;

   curCell.innerHTML = curValue + val;
})
<div id="SampleDiv">
   <table id="SampleTable" border=1>
     <tr>
       <td>First Name : </td>       
       <td>Middle Name : </td>       
       <td>Last Name : </td>       
     </tr>
</table>
</div>

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.