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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:36:46+00:00 2026-06-11T10:36:46+00:00

I want to add/remove rows in a table dynamically. I have javascript function to

  • 0

I want to add/remove rows in a table dynamically. I have javascript function to add and remove the rows. But, I want the delete button beside every single row so that I can delete a particular row.

ANd I want to add a row only if the first row is completely filled.

function to remove row

 function removeRowFromTable()
{
  var tbl = document.getElementById('tblSample');
  var lastRow = tbl.rows.length;
  if (lastRow > 2) tbl.deleteRow(lastRow - 1);
}

function to add rows:

function addRow(tableID) {

            var table = document.getElementById(tableID);

            var rowCount = table.rows.length;
            var row = table.insertRow(rowCount);

            var cell1 = row.insertCell(0);
            var element1 = document.createElement("input");
            element1.type = "text";
            cell1.appendChild(element1);

            var cell2 = row.insertCell(1);
            var element2 = document.createElement("input");
            element2.type = "text";
            cell2.appendChild(element2);
        }

my table:

<table id="tableId">
<tr><td>Host Name</td><td>Directory</td></tr>
<tr><td><input type="text"/></td><td><input type="text"/></td></tr>
<tr><td><input type="button" value="+" onclick="addRow(tableId)"/></td>
<td><input type="button" value="-" onclick="removeRowFromTable()"/></td></tr>
</table>

Any help is appreciated! Thanks in Advance!!!

  • 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-06-11T10:36:47+00:00Added an answer on June 11, 2026 at 10:36 am

    If you put a delete button on each row, then:

    <tr>
      <td><input type="button" value="Delete row" onclick="deleteRow(this)">
      <td><input type="text">
      <td><input type="text">
    </tr>
    

    And the deleteRow function can be:

    function deleteRow(el) {
    
      // while there are parents, keep going until reach TR 
      while (el.parentNode && el.tagName.toLowerCase() != 'tr') {
        el = el.parentNode;
      }
    
      // If el has a parentNode it must be a TR, so delete it
      // Don't delte if only 3 rows left in table
      if (el.parentNode && el.parentNode.rows.length > 3) {
        el.parentNode.removeChild(el);
      }
    }
    

    BTW, if all your rows have the same content, it will be much faster to add a row by cloning an existing row:

    function addRow(tableID) {
      var table = document.getElementById(tableID);
    
      if (!table) return;
    
      var newRow = table.rows[1].cloneNode(true);
    
      // Now get the inputs and modify their names 
      var inputs = newRow.getElementsByTagName('input');
    
      for (var i=0, iLen=inputs.length; i<iLen; i++) {
        // Update inputs[i]
      }
    
      // Add the new row to the tBody (required for IE)
      var tBody = table.tBodies[0];
      tBody.insertBefore(newRow, tBody.lastChild);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a simple html table where i want to delete and add rows
I have Panel with a BottomToolbar . I want to add and remove dynamically
I want to add row with remove button through javascript in html div formate.
I'm using jQuery to dynamically add rows to my HTML table on a button
i have 2 tables, i want to add rows from one table to another,
I want to dynamically add some columns (and remove others) from an html table.
I'm trying to add and remove some rows from a table using Jquery. But
I have created a table and some buttons to remove/add rows. Problem is, when
What I want to do is add and remove list items. I have got
Here in the below fiddle, i am trying to add and remove table rows

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.