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

The Archive Base Latest Questions

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

Hey all. I am adding a More Rows button to a table within a

  • 0

Hey all. I am adding a “More Rows” button to a table within a long, complex form, and to add the rows, I am cloning an existing row in the table, modifying it, then appending it to the end of the table.

The row contains inputs that have id and name attributes that are numbered sequentially (e.g. id=”Item_Name1″ and name=”Item_Name1″ for the first row, id=”Item_Name2″ and name=”Item_Name2″ for the second row, etc.). I am looking for the best way of continuing this numbering sequence in the appended rows.

Here’s the jQuery:

jQuery.noConflict();

jQuery(document).ready(function($){
  var i = $("table#table-id tbody tr").length;
  $("button#more-rows").click(function() {
      var clonedRow = $("table#table-id tbody tr:first").clone();
      i++;
      $("*[name*='1']", clonedRow).attr('id', function() {
          var attrval = $(this).attr("id");
          var attrval = attrval.replace( /\d/g , "" );
          return attrval + i
          });
      $("table#table-id").append(clonedRow);
  });
});

This works for the id’s, but how do I also increment the name attributes? Any tips on writing this more simply and/or more performantly are also appreciated.

  • 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-17T17:14:14+00:00Added an answer on May 17, 2026 at 5:14 pm

    This may or may not be helpful, but here goes:

    If you are using PHP, rather than suffixing the field names with a number, you might want to try naming them like “fieldname[]” and don’t give an ID. So you would have markup like this:

    <input type="text" name="Item_Name[]" />
    

    Then when you are checking the values on POST (another assumption I’m making), PHP will automatically create an array for that POST value, which you can access like so:

    <?php
    $ItemNames = $_POST['Item_Name'];
    foreach($ItemNames as $index => $value) {
       //values are ordered by the order they appeared in the DOM
       //each value in this array will be a value from an input that had name="Item_Name[]"
    }
    ?>
    

    OR

    If you’re not using PHP, what you’re doing would work fine. Just do the same thing for name that you’re doing for id.

    However, I would note that if you allow deleting of rows, in addition to incrementing based on the rowcount, you might want to make sure that an element with that given ID doesn’t already exist. Otherwise you could have a situation where you have two rows with fields suffixed with “1” & “2”, delete the first row and then add a new one, and then you’d have two rows with fields suffixed with “2”.

    Your code, with the name attribute set, and the id checked:

    jQuery.noConflict();
    
    jQuery(document).ready(function($) {
        var i = $("table#table-id tbody tr").length;
        $("button#more-rows").click(function() {
            var clonedRow = $("table#table-id tbody tr:first").clone();
            i++;
            $("*[name*='1']", clonedRow).attr('id', function() {
                var attrval = $(this).attr("id");
                attrval = attrval.replace(/\d/g, "");
                while($('#' + attrval + i).size() > 0) {
                    i++;
                }
                return attrval + i;
            }).attr('name', function() {
                var attrval = $(this).attr("name");
                attrval = attrval.replace(/\d/g, "");
                return attrval + i;
            });
            $("table#table-id").append(clonedRow);
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey all, my Computational Science course this semester is entirely in Java. I was
Hey all. Newbie question time. I'm trying to setup JMXQuery to connect to my
Hey all, I have something of an interesting requirement for my project. I need
Hey all. We're sending quite a few emails (around 23k) using IIS6 SMTP service
Hey all - I have an app where I'm authenticating the user. They pass
Hey all, I'm pulling my hair out on this one. I've checked all my
Hey all. I have a server written in java using the ServerSocket and Socket
Hey All! I'm trying to construct a query that is something like this: Where
Hey all, how can I reduce this: www.example.com/index.php?page=viewblog&category=awesome down to this: www.example.com/blog/awesome The above
Hey all, I'm trying to write a sort function but am having trouble figuring

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.