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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:25:53+00:00 2026-05-30T23:25:53+00:00

I am trying to clone a table row and generate an array with id

  • 0

I am trying to clone a table row and generate an array with id since the user could insert n rows. The problem I am facing is that I have 1 select dropdown option in that table row. How to clone a select along with the other input tags which are in 1 single row? (this code generates 2 sets of row at a time, coz of the two appendTo()’s. Thanks for the help!

$("#add").click(function() {
    $("#comTable tr:eq(0)").clone().find("input").each(function() {
        // creates array of ids if the user wants to add more than 1 row so it is N[1], etc
        $(this).val('').attr('id', function(_, id) {
            return id + 'N' + '[' + i + ']'
        });
        $(this).val('').attr('name', function(_, name) {
            return name + 'N' + '[' + i + ']'
        });
    }).end().appendTo("#comTable");

    $("#comTable tr:eq(0)").clone().find("select").each(function() {
        // creates array of ids if the user wants to add more than 1 row so it is N[1], etc
        $(this).val('').attr('id', function(_, id) {
            return id + 'N' + '[' + i + ']'
        });
        $(this).val('').attr('name', function(_, name) {
            return name + 'N' + '[' + i + ']'
        });
    }).end().appendTo("#comTable");
    i++;
});​
  • 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-30T23:25:54+00:00Added an answer on May 30, 2026 at 11:25 pm

    You can just select the input and the select element like so:

    Old Code:

    $("#comTable tr:eq(0)").clone().find("input").each(function() {
        // creates array of ids if the user wants to add more than 1 row so it is N[1], etc
        $(this).val('').attr('id', function(_, id) {
            return id + 'N' + '[' + i + ']'
        });
        $(this).val('').attr('name', function(_, name) {
            return name + 'N' + '[' + i + ']'
        });
    }).end().appendTo("#comTable");
    
    $("#comTable tr:eq(0)").clone().find("select").each(function() {
        // creates array of ids if the user wants to add more than 1 row so it is N[1], etc
        $(this).val('').attr('id', function(_, id) {
            return id + 'N' + '[' + i + ']'
        });
        $(this).val('').attr('name', function(_, name) {
            return name + 'N' + '[' + i + ']'
        });
    }).end().appendTo("#comTable");
    

    New Code:

    $("#comTable tr:eq(0)").clone().find("input, select").each(function() {
        // creates array of ids if the user wants to add more than 1 row so it is N[1], etc
        $(this).val('').attr('id', function(_, id) {
            return id + 'N' + '[' + i + ']'
        });
        $(this).val('').attr('name', function(_, name) {
            return name + 'N' + '[' + i + ']'
        });
    }).end().appendTo("#comTable");
    

    Note the "input, select" selector.

    Edit

    The other way you could do it, if you wanted to handle the selects differently is chain it differently:

    $("#comTable tr:eq(0)")
        .clone()
        .find("input")
        .each(function() {
                // creates array of ids if the user wants to add more than 1 row so it is N[1], etc
                $(this).val('').attr('id', function(_, id) {
                    return id + 'N' + '[' + i + ']'
                });
                $(this).val('').attr('name', function(_, name) {
                    return name + 'N' + '[' + i + ']'
                });
            })
        .end() //End .find("input")
        .find("select")
        .each(function() {
                // creates array of ids if the user wants to add more than 1 row so it is N[1], etc
                $(this).val('').attr('id', function(_, id) {
                    return id + 'N' + '[' + i + ']'
                });
                $(this).val('').attr('name', function(_, name) {
                    return name + 'N' + '[' + i + ']'
                });
            })
        .end() //End .find("select")
        .appendTo("#comTable");
    i++;
    

    This way removes the extra clone and runs .find again on the newly cloned DOM element.

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

Sidebar

Related Questions

I'm trying to clone a table row and update the multiple id's to reflect
I'm trying to use jquery to clone a table row everytime someone presses the
Trying to get my 1 table row to automatically clone exactly 24 times and
I'm trying to clone a row in a table, manipulate it a bit, and
I am trying to add a row to existing table by using clone() method.
I am trying to add a row to a table. I found that we
I have an html table that I am trying to add some animation to
I'm following this tutorial: http://www.javascripttoolbox.com/jquery/ And I'm trying to get table rows that are
The Situation I have a table in a DB that contains job types, basically
I am trying to update a specific row that uses auto_increment as a primary

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.