1

I'm VERY new to HTML, and any web development for that matter, so sorry if this question is super easy. In what I have done so far, I take a few inputs from users, do some calculations on them (using a javascript function I declared in the head tag), and spit out an answer. While doing the calculations, I store values in arrays, and I would like for the user to be able to see what is in these arrays. I want to display these values in tables. But, I have no idea how to put the values of the arrays in the display for the table. I'm aware that this is how a table works:

<table border="1">
<tr>
<td>row 1, cell 1</td>
<td>row 1, cell 2</td>
</tr>
<tr>
<td>row 2, cell 1</td>
<td>row 2, cell 2</td>
</tr>
</table>

But I would like to put my values where it says "row 1, cell 1" etc. From what I understand, the table tag has to be used in the body part of the HTML code (please tell me if I'm wrong!) so I don't know how to do this. I can't just create the table while calculating my values in my function, right? How can I access the arrays that I created and stored values in in my function? Any help is appreciated!

4
  • Please give us a example from the array you want to display as a HTML table. Commented Jan 7, 2013 at 2:59
  • Is what you want, an initial blank table, and then you dynamically add rows with data to it? Commented Jan 7, 2013 at 2:59
  • nope, the table will be 4 columns and 360 rows, so I understand that I can have a for loop running through the array, but how I access it is the question I guess? Commented Jan 7, 2013 at 3:00
  • give your cells ids, and then access them with jquery Commented Jan 7, 2013 at 3:01

2 Answers 2

1

You can create tables, actually, even with functions, but to simplify, lets use your table already there.

WORKING JSFIDDLE DEMO: http://jsfiddle.net/KEjpe/

JS:

var myjsonobject = { 
                    0: { 0: "row 1 cell 1", 1 : "row 1 cell 2"},
                    1: { 0: "row 2 cell 1", 1 : "row 2 cell 2"} 
                   }

var table = document.getElementsByTagName("table")[0];

var trs = table.getElementsByTagName("tr");

for(i=0;i<trs.length;i++)
{
   var tds = trs[i].getElementsByTagName("td");

   for(j=0;j<tds.length;j++)
   {
      tds[j].innerHTML = myjsonobject[i][j]
   }
}

HTML:

<table border="1">
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
</table>
Sign up to request clarification or add additional context in comments.

6 Comments

so as I'm calculating my values, I need to store them in this 2-d JSON object, and then do what you've given above?
ok i did it in jsfiddle to iron out any minor errors. I have fully working example now with jsfiddle it populates the table successfully with JSON stored values. I hope this helps you. Pure JS, no jQuery dependency!
so i just need to create the JSON object with the values I calculate, wow! thank you so much!
that's right you put it in your head <script> tag or you put it in an external js file loaded by <script src> tag, and the html just as is in the body tag. It will work and should output like in my jsfiddle demo
well, sorry I'm so new to this stuff and since you've been so helpful: how do I create the JSON object with the values in my arrays?
|
1

First you need a function to append tr and td to your table.

function addContentTo(table, myArr) {
    var frag = document.createDocumentFragment();
    var tr = document.createElement('tr');

    for (var col in myArr) {
        var td = document.createElement('td');
        td.innetHTML = col;
        tr.appendChild(td);
    }

    frag.appendChild(tr);
    table.appendChild(frag);
}

Now you can rum your lines array to populate your table:

var table = document.getElementById('mytable');
for(var line in myArray) {
    addContentTo(table, line);
}

I'm suposing that your array is something like this:

myArray = [
    ['row 1, cell 1', 'row 1, cell 2'],
    ['row 2, cell 1', 'row 2, cell 2']
];

Then your table will be constructed dynamic, but in my code case your table must have id 'mytable':

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

That's all folks.

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.