Web Analytics Made Easy -
StatCounter Need help creating table inside loop to add to web page - CodingForum

Announcement

Collapse
No announcement yet.

Need help creating table inside loop to add to web page

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Need help creating table inside loop to add to web page

    I am trying to create some paragraphs and two tables to append to a web page based on user input. I know the number of paragraphs in advance and just create new paragraph elements and new text nodes for them in advance. I do not know how many rows/cells will go into the table in advance as the number is based on the user input (number of spokes in a wheel as it turns out). I have tried a few different methods with little success.

    Here is the code I tried:

    Code:
    	var tblOHdr = document.createElement("h3");
    	tblOHdr.appendChild(document.createTextNode("Rotary table settings (\xB0) for the outer holes"));
    	div.appendChild(tblOHdr);
    	
    	var cellText = document.createTextNode("");
    	var t = theta2;
    	var k = 0;
    	while (t < 360) {
    		cellText.textContent = t;
    		cell.appendChild(cellText);
    		row.appendChild(cell);
    		tblBody.appendChild(row);
    		
    		if (k) {
    			t = t + 2 * theta2;
    		}
    		else {
    			t = t + sep;
    			k = Math.pow(k,1);
    		}
    	}
    	tbl2.appendChild(tblBody);
    	div.appendChild(tbl2);
    This produces one row with one cell containing the last t calculated. It seems like somehow I need to create new elements each time through the loop? Thanks for any input or suggestions.

    John

  • #2
    See Creating Tables for an example of how to create a table using JavaScript.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

    Comment


    • #3
      Thanks again felgall for a great suggestion. Fitting the code to my needs was quick and worked like a champ. I am not familiar with innerHTML, but will learn how to use it now.

      Have a great evening.

      John

      Comment


      • #4
        You can still create the content of the cells using createTextNode. The example is showing how to create the tags for the table.
        Stephen
        Learn Modern JavaScript - http://javascriptexample.net/
        Helping others to solve their computer problem at http://www.felgall.com/

        Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

        Comment

        Working...
        X