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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:38:38+00:00 2026-05-17T18:38:38+00:00

I’m trying to write a function in jQuery that will split lists – or

  • 0

I’m trying to write a function in jQuery that will split lists – or really, any element that has children – that is conscious of memory use and is general enough to be widely reusable, but I’m having trouble resolving these against one another.

For the sake of argument (and to keep the code on point) let’s say I’m trying to split the list into two lists after element three. I’m not checking my code for syntax errors or anything else – hopefully you get the idea of what I’m trying to do.

Option one is to write a function like this:

var list = $('ul.listCopy');
var newList = $('<ul class="listCopy" />');
var latterElements = list.children('li:gt(3)').remove();
newList.append(latterElements);
list.after(newList);

This is great, but it only works so long as I know the type and attributes of the element. I would have to write a separate function if I were splitting a div with multiple divs inside it, or an OL, or whatever. If the element has a class or a role or any other html attribute, I want to be able to copy it without worrying about what kind of custom attributes might come up in the future. I want to make this general enough to handle any sort of element. This is option two:

var list = //something
var newList = list.clone();
newList.children(':lt(4)').remove();
list.children(':gt(3)').remove();

This is general enough that I could make a copy of anything, but the problem is that I’m making a copy of the entire list. If I were to need to use this program to divide a particularly long list, I’m worried about the memory implications.

The ideal thing I’d like to do is make a copy of an element but only copy the element itself, with none of its children. I don’t want to have to copy anything I’m going to end up deleting. Is there a way to do something like this? Most of the plugins and functions I’ve seen choose one way or the other.

Thanks!

  • 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-17T18:38:38+00:00Added an answer on May 17, 2026 at 6:38 pm

    Use the native cloneNode() method. If you don’t pass true as an argument, it doesn’t clone any children.

    Example: http://jsfiddle.net/4rYxE/

        // Original element
    var list = $('ul');
        // Clone of original (top level only)
    var newList = list[0].cloneNode();
        // Append children greater than index 3 to the new element
    list.children(':gt(3)').appendTo(newList);
    

    Using .appendTo() removes the elements from the original list, and moves them to their new location (in this case, the newList).

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

Sidebar

Related Questions

No related questions found

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.