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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:30:53+00:00 2026-06-09T22:30:53+00:00

I have an HTML Table and would like to allow the user to click

  • 0

I have an HTML Table and would like to allow the user to click a button to add a new Row, but the new Row will not be added at the end of the table but rather after the last row of input fields.

I’ve setup a sample JSFiddle at:

http://jsfiddle.net/n7Gup/2/

I have a New Row button but it’s not cloning the last row of inputs (i.e the row where the New Row button is). I need to modify this so that it will insert a new row after the last of these input rows.

Here’s the script which I found from an online tutorial:

$(document).ready(function($)
{
  // trigger event when button is clicked
  $("#button2").click(function()
  {
    // add new row to table using addTableRow function
    addTableRow($("#nextYear"));

    // prevent button redirecting to new page
    return false;
  });

  // function to add a new row to a table by cloning the last row and
  // incrementing the name and id values by 1 to make them unique
  function addTableRow(table)
  {
    // clone the last row in the table
    var $tr = $(table).find("tbody tr:last").clone();

    // get the name attribute for the input and select fields
    $tr.find("input,select").attr("name", function()
    {
      // break the field name and it's number into two parts
      var parts = this.id.match(/(\D+)(\d+)$/);

      // create a unique name for the new field by incrementing
      // the number for the previous field by 1
      return parts[1] + ++parts[2];
    // repeat for id attributes
    }).attr("id", function()
    {
      var parts = this.id.match(/(\D+)(\d+)$/);
      return parts[1] + ++parts[2];
    });

    // append the new row to the table
    $(table).find("tbody tr:last").after($tr);
  };
});

The new row that is created can have all the input fields empty as the user will start again. Also is there a way to have the New Row button only appear once, and that will always be at the last row of activity inputs. For example initially there will only be one row, but if you click the New Row button a 2nd row will be created below the initial row and the New Row button will now only appear on this newly created row.

Appreciate any help – I’m new to Javascript.

  • 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-09T22:30:54+00:00Added an answer on June 9, 2026 at 10:30 pm

    Here is a solution

    http://jsfiddle.net/7vuZf/3/

    Basically you simply pass the reference to the the button clicked and work out your positioning in the table from there – save the reference to the current last row (using closest(), clone it (with events), remove the new row button in the original (you probably need to replace it with a delete button), and insert the clone after the original.

    Also now you probably do not need a reference to the table object passed to your click handler (I left it in the code though).

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

Sidebar

Related Questions

I have a html table and insert a new row with table.insertRow() , but
I have a HTML table and I would like to change the table to
I have HTML table. I'm looping through table and iterate over each row whose
see fiddle i have html table and one textbox and one button.make cell selection
I have a html table like this: Invoice# Due Date Balance Select 12345 12/25/2011
I have an HTML table like: <div> <table border=1> <thead> <tr class=example> <th> <span
I'm new to MVC and would like to add a link to something like
I have an HTML table displaying a lot of columns in a single row
I have a html table structure like this; <tr style=font-weight: bold> <td>ID</td> <td>Navn</td> <td>Adresse</td>
I have an HTML table in my jsp page. This conains text fields and

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.