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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:36:55+00:00 2026-05-25T19:36:55+00:00

I’m having some syntax understanding issues. I’m trying to take a div, clone it,

  • 0

I’m having some syntax understanding issues.

I’m trying to take a div, clone it, wrap the clone in two new generated divs, and then stick it all into the DOM right before the closing BODY tag. This is what I have:

$('.myDiv').click(function(){
    var $myDiv = $(this).clone();

    var $myWrapper1 = $('div').css('border','10px solid blue');
    var $myWrapper2 = $('div').css('border','10px solid green');

    $myDiv.wrap($myWrapper1).wrap($myWrapper2).appendTo('body');
});

jsbin live example: http://jsbin.com/upedox/4/

This isn’t doing what I am expecting it to do, so clearly I’m not fully understanding wrap. What I want to happen is to end up with a copy of my div (red border) with two divs wrapping it (one with a green and blue border) and for that to then be inserted after the existing div.

What is instead happening is that the div gets cloned, but isn’t wrapped with the other divs but, rather, it’s content is just inserted into the last div I wrap it with (so I end up with a cloned green border div) and it sticks it above the existing div.

What am I misunderstanding with wrap?

  • 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-25T19:36:55+00:00Added an answer on May 25, 2026 at 7:36 pm

    As I explained, picture the following:

    <div class="foo">Bar</div>
    

    If you want to wrap foo in a bar div, you’d call it:

    var $r = $('.foo').wrap($('<div>',{class:'bar'}));
    

    Instead of $r resulting in <div class="bar"><div class="foo">Bar</div></div> (as you’re expecting) what you’re actually getting back is the original <div class="foo">Bar</div> (and inside the DOM it’s been wrapped with <div class="bar"></div>).

    So, instead (considering you’re cloning it and not working directly in DOM) use append instead:

    var $orig = $('.foo').clone();
    
    var $firstWrap = $('<div>',{class:'bar'});
    var $secondWrap = $('<div>',{class:'baz'});
    
    var $r = $secondWrap.append($firstWrap.append($orig));
    

    Now, the above has <div class="baz"><div class="bar"><div class="foo">Bar</div></div></div> inside (like you’re desiring) which you can then appendTo('body')

    live demo

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have just tried to save a simple *.rtf file with some websites and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string

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.