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

  • Home
  • SEARCH
  • 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 8531931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:35:47+00:00 2026-06-11T09:35:47+00:00

I need some help figuring out how to write some jQuery code. I need

  • 0

I need some help figuring out how to write some jQuery code.

I need to clone a table dynamically onclick. but then I need to change the ids of the table and its child elements each time. as the table can have lots of children it will be to difficult to do this manually. I need a way to change the id’s of all child(all descendants) elements. I will always just add the counter to the id. (I know children will access only the immediate children but I just wanted to try if that works). If you guys know how this can be done in either jQuery or Javascript, please let me know.

<table id="table"> 
    <tr id = "tr" > 
        <td id="td"> 
            <span id="span" value="hiii">hi</span> 
        </td> 
    </tr>
</table> 

<button>Clone</button> 

<script> 
    $("button").click(function () { 
        var table = $("#table").clone(true,true) 
        table.attr( 'id', function() { return this.id +"1"; })    
        alert($("#table1").children()) 
        $("#table1").find(*).attr('id', function() { return this.id +"1"; }) 
        table.appendTo("#table") 
        alert(document.getElementById("tr1")) 
        alert(document.getElementById("span1").value) 
    }); 
</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-06-11T09:35:48+00:00Added an answer on June 11, 2026 at 9:35 am

    If elem is the parent of your cloned structure and cntr is the counter you said you were maintaining, you can fix all ids in that cloned structure like this:

    function fixIds(elem, cntr) {
        $(elem).find("[id]").add(elem).each(function() {
            this.id = this.id + cntr;
        })
    }
    

    If the ids might already have a cloned number at the end and you want to replace that number, you can do so like this:

    function fixIds(elem, cntr) {
        $(elem).find("[id]").add(elem).each(function() {
            this.id = this.id.replace(/\d+$/, "") + cntr;
        })
    }
    

    So, based on the code you’ve now added to your question, you could do this:

    <script> 
        var cloneCntr = 1;
        $("button").click(function () { 
            var table = $("#table").clone(true,true) 
            fixIds(table, cloneCntr);
            table.insertAfter("#table") 
            cloneCntr++;
        });
    </script>
    

    Working example: http://jsfiddle.net/jfriend00/wFu9K/

    Note: I also changed the DOM insertion code to insertAfter(), because you can’t append one table to another the way you were doing (a table would either go after the existing table or inside a cell in the previous table).


    If you are just trying to add a row, then you need to clone the row, not the whole table.

    You could add a cloned row to the existing table with this code:

    function fixIds(elem, cntr) {
        $(elem).find("[id]").add(elem).each(function() {
            this.id = this.id.replace(/\d+$/, "") + cntr;
        })
    }
    var cloneCntr = 1;
    $("button").click(function () { 
        var row = $("#table tr").first().clone(true,true);
        fixIds(row, cloneCntr++);
        row.appendTo("#table") 
    }); ​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some help figuring out how to write this .htaccess . My file
I'm doing an ASP.Net MVC website, but I need some help figuring out how
So I'm disassembling some code (binary bomb lab) and need some help figuring out
I need some help figuring out how I should do this. I would like
Hey all, i am in need of some help with figuring out how to
I need some help figuring out how to do something. I got this gallery
I have a CKEditor custom plugin I'm trying to write, but need some help
I need some help figuring out a query I have 3 tables sources id,
I'm a n00b and I need some help figuring out this issue, I would
I'm looking to implement the Shunting-yard Algorithm , but I need some help 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.