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 6931787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:37:21+00:00 2026-05-27T11:37:21+00:00

I have read many articles so far and tried so many different ways to

  • 0

I have read many articles so far and tried so many different ways to add rows dynamically to my HTML table, but nothing is working. When I click the add button [ + ], it’s not adding a row. For some reason looks like it’s not letting me add cells too, when I am do: newCell = newRow.insertCell(). My newRow variable can’t find the insertCell() method.

This is my table:

<table ID="newDataTable" runat="server" width="400">
<tr runat="server"> 
    <td runat="server" nowrap="nowrap" bordercolor="#FFFFFF" width= "50">
        <label style="font-size:smaller"> CITY: </label>
    </td>
    <td runat="server" nowrap="nowrap" bordercolor="#FFFFFF" width= "80">
        <input id= "City_box" type="text" name="tb_CITY"/>
    </td>
    <td runat="server" nowrap="nowrap" bordercolor="#FFFFFF" width= "75">
        <label style="font-size:smaller"> &nbsp;&nbsp;&nbsp;STATE: </label>
    </td>
    <td runat="server" nowrap="nowrap" bordercolor="#FFFFFF" width= "90">
        <input id= "State_box" type="text" name="tb_STATE"/>
    </td>
    <td runat="server" align="left" bordercolor="#FFFFFF" width= "10">
        <input title="Add Row" id="addButton" type="button" value=" + " onclick="addRow()" />
    </td> 
    <td runat="server" align="left" bordercolor="#FFFFFF" width= "10">
        <input title="Remove Row" id="deleteButton" type="button" value=" - " onclick="removeRow(this)" />
    </td> 
</tr>
</table>

And here is the Javascript I am using:

<script type="text/javascript">
//add a new row to the table
function addRow() 
{
    //add a row to the rows collection and get a reference to the newly added row
    var newRow = document.getElementById('newDataTable').insertRow(-1);

    //add 3 cells (<td>) to the new row and set the innerHTML to contain text boxes

    var newCell = newRow.insertCell();
    newCell.innerHTML = "<label style='font-size:smaller'> CITY: </label>";

    newCell = newRow.insertCell();
    newCell.innerHTML = "<input type='text' name='tb_CITY'/>";

    newCell = newRow.insertCell();
    newCell.innerHTML = "<label style='font-size:smaller'> &nbsp;&nbsp;&nbsp;STATE: </label>";

    newCell = newRow.insertCell();
    newCell.innerHTML = "<input type='text' name='tb_STATE'/>";

    newCell = newRow.insertCell();
    newCell.innerHTML = "<input title='Add Row' name='addButton' type='button' value=' + ' onclick='addRow()' />";

    newCell = newRow.insertCell();
    newCell.innerHTML = "<input title='Remove Row' name='deleteButton' type='button' value=' - ' onclick='removeRow(this)' />";
}

//deletes the specified row from the table
function removeRow(src) 
{
    /* src refers to the input button that was clicked. 
    to get a reference to the containing <tr> element,
    get the parent of the parent (in this case <tr>)
    */
    var oRow = src.parentElement.parentElement;

    //once the row reference is obtained; delete it passing in its rowIndex
    document.all("newDataTable").deleteRow(oRow.rowIndex);
}    
</script>
  • 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-27T11:37:22+00:00Added an answer on May 27, 2026 at 11:37 am

    The problem is that you can’t have both a src attribute and code in a <script> element (OP edited this in the question, so this refers to the previous code that wasn’t working).

    Change:

    <script type="text/javascript" src="script/FloatLayer.js">
    

    To:

    <script type="text/javascript">
    

    Or, move addRow() and removeRow() to FloatLayer.js. Here’s a demo where at least the add button is working with no code change.

    Demo: http://jsfiddle.net/ThinkingStiff/9aDpf/

    I changed insertCell() to insertCell( -1 ) to put the elements in the right order, which is probably what you wanted.

    Also, as Jonathan M mentioned, to get it to work in all browsers, change:

    insertRow()

    To:

    insertRow( -1 )

    One final note: You can use tr.cloneNode( true ) to achieve the same results with much less code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've read many different articles/threads on this, but have yet to determine the best
I am interested in changing the structure folder. I have read many articles, but
I have read many about REST api in php articles. but I still get
In many articles, tutorials, docs, have read so far, that we call startService() or
I have read many articles about vertical centering but I’m afraid not many of
I have read many articles regarding layout, but I am still quitely confused. My
I have read many MSDN articles about assembly signing and I haven't found nothing
String str = new String(Hello); Normally I have read, in many articles available on
In many of the articles I have read on the web say that when
I dont get 1-n relationship. I have read some articles but still cant figure

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.