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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T03:18:05+00:00 2026-05-22T03:18:05+00:00

I’m trying to take content from a list (but not including the LI tag

  • 0

I’m trying to take content from a list (but not including the LI tag wrapped around it) and copy it elsewhere.

I’ve got this fiddle here: http://jsfiddle.net/csaltyj/u5tFb/

I simply cannot figure this out. How do I get this working right?

HTML:

<div id="container">
</div>

<ul>
   <li><p>Text 1</p><em>Text 2</em></li>
   <li>Gonna copy me too?</li>
   <li><h3>We're gonna be in DIVs!</h3></li>
</ul>

<button id="copy">Copy</button>
<button id="clear">Clear DIV</button>

CSS:

#container {
    background: #eee;
}

#container .special {
    border: 1px solid red;
}

li {
    list-style-type: disc;
    margin: 1em;
}

jQuery:

$('#copy').click(function() {
    $('li').each(function(i) {
        $(this).clone().contents().wrap('<div class="special" />').appendTo('#container');
    });
});

jQuery:

  • 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-22T03:18:06+00:00Added an answer on May 22, 2026 at 3:18 am

    I believe your problem is that you’re assuming wrap returns the wrapper. It’s a perfectly reasonable assumption, but it’s incorrect; it returns the original elements, not the wrapper. So when you append them, it’s as though you hadn’t put them in a wrapper, as the elements are moved out of it and the wrapper is thrown away.

    I suspect this is more what you’re looking for:

    $('#copy').click(function() {
        $('li').each(function(i) {
            $("<div class='special'>")
                .append($(this).clone().contents())
                .appendTo('#container');
        });
    });
    

    Updated fiddle

    Though it would be marginally more efficient to use $(this).contents().clone() rather than $(this).clone().contents() since you don’t use the cloned wrapper li, just the cloned contents:

    $('#copy').click(function() {
        $('li').each(function(i) {
            $("<div class='special'>")
                .append($(this).contents().clone())
                .appendTo('#container');
        });
    });
    

    Updated fiddle

    Either way, those put the two elements in your first li in one wrapper div, which I suspect is what you want. Alternately, though, if you really want to wrap each part of what’s in each li separately, just move up to the parent element before doing your final append (but that splits out every element in each li into its own div, which I don’t think is what you want):

    $('#copy').click(function() {
        $('li').each(function(i) {
            $(this)
                .clone()
                .contents()
                .wrap('<div class="special" />')
                .parent()  // <=== The new bit
                .appendTo('#container');
        });
    });
    

    Updated fiddle

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

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I need to clean up various Word 'smart' characters in user input, including but
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:

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.