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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:34:21+00:00 2026-05-13T23:34:21+00:00

//dom <div id=’toBeCloned’><span>Some name</span></div> <div id=’targetElm’></div> //js $(function () { //creating a clone var

  • 0
    //dom    

    <div id='toBeCloned'><span>Some name</span></div>
    <div id='targetElm'></div>

    //js
        $(function () {
            //creating a clone
            var _clone = $('#toBeCloned').clone(true);
    // target element
            var _target = $('#targetElm');

    //now target element is to be filled with cloned element with data of span changed
            var _someData = [1, 2, 3, 4];

    //loop through data
            $.each(_someData, function (i, data) {
                var _newElm = {};
                $.extend(_newElm, _clone);//copy cloned to  new Elm
                _newElm.find('span').html(data); //edit content of span
                alert('p'); // alert added to show that append in next line inspite of adding new element to dom just updating the previous one
                _target.append(_newElm);//update target
            });
        });


expected Result:
1 2 3 4
resut iam getting is
4
  • 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-13T23:34:21+00:00Added an answer on May 13, 2026 at 11:34 pm

    You just want to clone the element on each iteration. A DOM object is not a normal JS object (Well it is, but it has more to it which is why you have to use createElement in raw JS to create a new one):

    $(function () {
        // element to be cloned
        var _clone_me = $('#toBeCloned');
        // target element
        var _target = $('#targetElm');
    
        //now target element is to be filled with cloned element with data of span changed
        var _someData = [1, 2, 3, 4];
    
        //loop through data
        $.each(_someData, function (i, data) {
            var _newElm = _clone_me.clone(true); // clone copy
            _newElm.find('span').html(data);  //edit content of span
            _target.append(_newElm); //update target
        });
    });
    

    Why your code failed, is though you were “extending” an empty object on each iteration, it was still always referring to the same DOM node. You kept changing its value, and appending it to #target. Additionally, the way append works actually moves the object, so you weren’t getting duplicate objects.

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

Sidebar

Related Questions

I have the following function to remove a DOM element div, $('#emDiv').on(click, ':button[data-emp-del=true]', function
i got these two in the DOM: <div id=buffert> <span class=left><a>link</a><a>link</a></span> <span class=right>Some text<span>title</span></span>
i'm creating a new dom element: var bubbleDOM = document.createElement('div'); bubbleDOM.setAttribute('class', 'selection_bubble'); document.body.appendChild(bubbleDOM); then
i am creating div using DOM Element as phototab = document.createElement('div'); phototab.setAttribute('class', 'phototab fleft');
I have a DOM tree: <div id=all> <div id=second> <div id=target></div> * <div id=somediv></div>
I have a dom element like div , span (with display:inline-block ), whose width
i have the following DOM elements: <div class=document> <div class=text> some text1 </div> <div
I created a DOM fragment using JQuery: var $content = $(<div /, {id:content}) I
My dom looks like: <div id=foo> </div> <ul> <li>...</li> <li>..</li> </ul> I'm performing some
Within a particular div/span that I might dynamically insert into the DOM, I need

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.