Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1081973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:14:17+00:00 2026-05-16T22:14:17+00:00

I am trying to insert rows into an html table with javascript. I have

  • 0

I am trying to insert rows into an html table with javascript. I have the table created and can add rows successfully with the click of a button. However, I want to be able to populate the rows with elements from several arrays on load. The button is just to make sure the rows are being added.

<html>
 <input type="button" value="Add row" onclick="javascript:appendRow()"     class="append_row"/>
 <br/><br/></p>
 <table id="my_table" align="center" cellspacing="0" cellpadding="0" border="0">
 <tr>
<td>Name</td>
<td>Age</td>
 <td>Sex</td>
 </tr>

</table>
<p></center></p>

 <script type="text/javascript" language="javascript">
 function appendRow(){


var names = ["Paul", "Mike", "Linda"];
var ages = ["16", "23", "44"];
var male_female = ["M", "M", "F"];
var tbl = document.getElementById('my_table'); // table reference
// append table row
var row = tbl.insertRow(tbl.rows.length);

// insert table cells to the new row
   var row = tbl.insertRow();

for(var i=0;i<tbl.rows[0].cells.length;i++)
    createCell(row.insertCell(i), i, 'row');

 }

function createCell(cell, text, style){
var div = document.createElement('div'); // create DIV element
var txt = document.createTextNode(text); // create text node
div.appendChild(txt);                    // append text node to the DIV
div.setAttribute('class', style);        // set DIV class attribute
div.setAttribute('className', style);    // set DIV class attribute for IE (?!)
cell.appendChild(div);                   // append DIV to the table cell
}
</script>
<style>
 .row{background-color:#FFD6D6;width:43px;margin:3px;}
 .col{background-color:#D6FFD6;width:43px;margin:3px;}
  table#my_table{border-collapse:collapse;}
    table#my_table td{width:50px;height:27px;border:1px solid #D3D3D3;font-  size:10pt;text-align:center;padding:0;}
   .append_row{background-color:#FFD6D6;border:1px #ccc solid;}
   .append_column{background-color:#D6FFD6;border:1px #ccc solid;}
   .delete{background-color:#eee;border:1px #ccc solid;}
   </style>
   </html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T22:14:18+00:00Added an answer on May 16, 2026 at 10:14 pm

    In your for loop, try something like:

    var j = tbl.rows.length - 1;
    for (var i=0;i<tbl.rows[0].cells.length;i++) {
      var cell_text = '';
      if (i == 0) {
         cell_text = name[j];
      } else if (i == 1) {
         cell_text = ages[j];
      } else if (i == 2) {
         cell_text = male_female[j];
      }
      createCell(row.insertCell(i), cell_text, 'row');
    
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to insert thousands of rows into a table and performance is
I am trying to insert rows into a table that has no unique field
I am trying to insert rows of data in an array into a table.
I an trying to INSERT multiple rows into an SQL, Oracle table though SQL
Ok I have a temporary table with 135000 rows, I'm trying to insert some
I am trying to insert 2 rows into the same table. The first will
I am trying to insert over 70,000 rows into a javascript database (using Chrome
I'm trying to use SqlBulkCopy to insert new rows into my DB table by
I am trying to insert multiple rows into MySQL DB using PHP and HTML
I'm trying to insert additional rows into a table which requires a value to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.