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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:45:50+00:00 2026-06-11T00:45:50+00:00

I am trying to clone multiple divs on my page by using the jQuery

  • 0

I am trying to clone multiple divs on my page by using the jQuery .clone() method. The problem is, as soon as a div is cloned, it needs to have a unique ID. The cloned ID has to be there too. I was thinking I could keep the old ID and then just add a number on, increasing as more div’s are on the page.

Example: base ID = one, so div one would be id, then div two would be id-2, then div three would be id-3, etc.

Is this possible? My attempt at this is below:

$("a").click(function(){
        var target = $(this).attr("href");
        var id = $(target).attr("id");
        $(target).clone().attr("id",id + $(id).size()).attr("class","drag").appendTo("body");
});

Each a tag looks like this:

<a href="#one">One</a>
<a href="#two">Two</a>

Then the cloned element looks like this:

<div class="drag base" style="background-color:blue" id="one"></div>
<div class="drag base" style="background-color:green" id="two"></div>
  • 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-11T00:45:52+00:00Added an answer on June 11, 2026 at 12:45 am

    See this: http://jsfiddle.net/3tu7V/1/

    $(function(){
        $("a").click(function(){
                var target = $(this).attr("href");
                var id = $(target).attr("id"); 
                var click = $(target).data("clicked") || 0;
                $(target).data("clicked", ++click);            
                $(target).clone().attr("id",id + click).attr("class","drag").appendTo("body");
    
        });
    });
    ​
    

    I think this does what you want according to your comment:
    “Ah, is there any way for the element ID to be reset when the base ID is unique? Ex.) “If you clone div “one”, it will produce “one-1”, then “one-2”, but if you then clone div “two”, it will produce “two-3”, not “two-1″”

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

Sidebar

Related Questions

I'm trying to make system of multiple dialogs in one page using jquery dialog...
I am attempting to clone fields in a form using Jquery. I'm trying to
I'm trying to scan multiple ports at once using asynchronymous scanning. The problem is
I have a page with multiple popovers that I am trying to dismiss when
I'm trying to clone my personal fork on github using the git+ssh protocol with
I'm using Isapi Rewrite 3 (mod rewrite clone for IIS) and trying to rewrite
I'm trying to clone a table row and update the multiple id's to reflect
hope can help, trying out Jquery clone which seems to work, but I get
I am trying to geocode multiple addresses in a model using either geokit or
I am trying to write a class that will calculate checksums using multiple processes,

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.