Wrap groups of elements using jQuery

Russell Pabon

I've got a list of elements and I want to group them with div starting with the first element and ends with BREAK class then start a new group after the class BREAK.

So the code below,

<div class="test">Test 0</div>
<div class="test">Test 1</div>
<div class="break">Break 0</div>
<div class="test">Test 2</div>
<div class="test">Test 3</div>
<div class="break">Break 1</div>
<div class="test">Test 4</div>
<div class="test">Test 5</div>
<div class="test">Test 6</div>

Would become,

<div>
    <div class="group">
        <div class="test">Test 0</div>
        <div class="test">Test 1</div>
        <div class="break">Break 0</div>
    </div>
    <div class="group">
        <div class="test">Test 2</div>
        <div class="test">Test 3</div>
        <div class="break">Break 1</div>
    </div>
        <div class="group">
        <div class="test">Test 4</div>
        <div class="test">Test 5</div>
        <div class="test">Test 6</div>
        <div class="break">Add last break element</div>
    </div>
<div>

And append a div element in the last group.

Any ideas? Thanks in advance.

dfsq

I think you can also do something like this: first you insert one more .break to all .test parent container, then using wrapAll method wrap groups properly:

$('.test').parent().append('<div class="break">Break 2</div>').
find('.test + .break').each(function() {
    $(this).prevUntil('.break, .group').add(this).wrapAll('<div class="group"></div>');
});
.group {
  padding: 3px;
  background: #BFECBE;
  margin-bottom: 2px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="container">
    <div class="test">Test 0</div>
    <div class="test">Test 1</div>
    <div class="break">Break 0</div>
    <div class="test">Test 2</div>
    <div class="test">Test 3</div>
    <div class="break">Break 1</div>
    <div class="test">Test 4</div>
    <div class="test">Test 5</div>
    <div class="test">Test 6</div>
</div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Wrap groups of elements using jQuery

From Dev

Wrap around groups of elements using jQuery

From Dev

How to wrap groups of two elements in jQuery?

From Dev

Using jQuery to wrap elements in a hyperlink

From Dev

Using jQuery to wrap elements in a hyperlink

From Dev

Wrap elements inside another using classes in jQuery

From Dev

Wrap group of elements in multiple HTML elements using JQuery

From Dev

jQuery: Wrap elements with nextUntil()

From Dev

Wrap elements into div with jQuery

From Dev

Wrap tags around items inside existing <li> elements using jQuery

From Dev

Using jquery / javascript to wrap a series of HTML elements in Wordpress

From Dev

Jquery wrap elements inside div

From Dev

Wrap function Jquery for multiple elements

From Dev

How to wrap all HTML elements in a DIV depending on total height of those HTML elements using jQuery?

From Dev

wrap $ in span using jQuery

From Dev

How do wrap these groups of li elements in ul containers?

From Dev

How to wrap elements in a group of items with jQuery?

From Dev

jQuery wrap multiple elements independent of level

From Dev

Wrap div around 2 sequential elements in jQuery

From Dev

Wrap a div around every 2 elements in jQuery

From Dev

Wrap multiple selected elements with a div with jquery

From Dev

Is using a HTML Label to wrap elements semantic?

From Dev

HAML iterate over collection - how to wrap groups of child elements in unique parents?

From Dev

Creating elements using jQuery

From Dev

Wrap substring of element content with an element using jQuery

From Dev

Wrap an img within a div using jquery

From Dev

Wrap deferents html tags with a div using jQuery

From Dev

Using Jquery wrap function with absolute placed element

From Dev

Wrap each line text to an element using JQuery

Related Related

  1. 1

    Wrap groups of elements using jQuery

  2. 2

    Wrap around groups of elements using jQuery

  3. 3

    How to wrap groups of two elements in jQuery?

  4. 4

    Using jQuery to wrap elements in a hyperlink

  5. 5

    Using jQuery to wrap elements in a hyperlink

  6. 6

    Wrap elements inside another using classes in jQuery

  7. 7

    Wrap group of elements in multiple HTML elements using JQuery

  8. 8

    jQuery: Wrap elements with nextUntil()

  9. 9

    Wrap elements into div with jQuery

  10. 10

    Wrap tags around items inside existing <li> elements using jQuery

  11. 11

    Using jquery / javascript to wrap a series of HTML elements in Wordpress

  12. 12

    Jquery wrap elements inside div

  13. 13

    Wrap function Jquery for multiple elements

  14. 14

    How to wrap all HTML elements in a DIV depending on total height of those HTML elements using jQuery?

  15. 15

    wrap $ in span using jQuery

  16. 16

    How do wrap these groups of li elements in ul containers?

  17. 17

    How to wrap elements in a group of items with jQuery?

  18. 18

    jQuery wrap multiple elements independent of level

  19. 19

    Wrap div around 2 sequential elements in jQuery

  20. 20

    Wrap a div around every 2 elements in jQuery

  21. 21

    Wrap multiple selected elements with a div with jquery

  22. 22

    Is using a HTML Label to wrap elements semantic?

  23. 23

    HAML iterate over collection - how to wrap groups of child elements in unique parents?

  24. 24

    Creating elements using jQuery

  25. 25

    Wrap substring of element content with an element using jQuery

  26. 26

    Wrap an img within a div using jquery

  27. 27

    Wrap deferents html tags with a div using jQuery

  28. 28

    Using Jquery wrap function with absolute placed element

  29. 29

    Wrap each line text to an element using JQuery

HotTag

Archive