wrap all elements between two elements

Pindo

I've got the following HTML

<div class="bar-something"><div class="fn-label"> SOMETHING</div> </div>
<div class="bar-appointment"><div class="fn-label"> Ap1</div></div>
<div class="bar-appointment"><div class="fn-label"> Ap2</div></div>
<div class="bar-appointment"><div class="fn-label"> Ap3</div></div>
<div class="bar-something-else"><div class="fn-label"> SOMETHING</div></div>
<div class="bar-appointment"><div class="fn-label"> Ap1</div></div>
<div class="bar-appointment"><div class="fn-label"> Ap2</div></div>
<div class="bar-appointment"><div class="fn-label"> Ap3</div></div>
<div class="bar-something-odd"><div class="fn-label"> SOMETHING</div></div>

I want to wrap all the bar-appointment elements with another DIV so the DOM would then look like:

<div class="bar-something"><div class="fn-label"> SOMETHING</div> </div>
<div class="test">
  <div class="bar-appointment"><div class="fn-label"> Ap1</div></div>
  <div class="bar-appointment"><div class="fn-label"> Ap2</div></div>
<div>
<div class="bar-something-else"><div class="fn-label"> SOMETHING</div></div>
<div class="test">
    <div class="bar-appointment"><div class="fn-label"> Ap1</div></div>
    <div class="bar-appointment"><div class="fn-label"> Ap2</div></div>
    <div class="bar-appointment"><div class="fn-label"> Ap3</div></div>
    <div class="bar-appointment"><div class="fn-label"> Ap4</div></div>
    <div class="bar-appointment"><div class="fn-label"> Ap5</div></div>
</div>
<div class="bar-something-odd"><div class="fn-label"> SOMETHING</div></div>

I've tried the following but all the inner tags get wrapped too and I don't want that.

$('.bar-appointment').each(function() {
  $(this).nextUntil("div[class!='bar-appointment']").andSelf().wrapAll('<div class="test"></div>')});

I also tried using just wrap and this only wraps each element individually, not as a group as I would like.

What's the best way of me doing this?

Arun P Johny

First you can find all the first bar-appointment element(of the set) by removing bar-appointment elements which is having previous sibling as bar-appointment. Then iterate over that and find all the sibling which are bar-appointment elements and wrap all of them

$('.bar-appointment').not('.bar-appointment + .bar-appointment').each(function() {
  $(this).nextUntil(":not(.bar-appointment)").addBack().wrapAll('<div class="test"> </div>')
})
.test {
  border: 1px solid red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="bar-something"><div class="fn-label"> SOMETHING</div> </div>
<div class="bar-appointment"><div class="fn-label"> Ap1</div></div>
<div class="bar-appointment"><div class="fn-label"> Ap2</div></div>
<div class="bar-appointment"><div class="fn-label"> Ap3</div></div>
<div class="bar-something-else"><div class="fn-label"> SOMETHING</div></div>
<div class="bar-appointment"><div class="fn-label"> Ap1</div></div>
<div class="bar-appointment"><div class="fn-label"> Ap2</div></div>
<div class="bar-appointment"><div class="fn-label"> Ap3</div></div>
<div class="bar-something-odd"><div class="fn-label"> SOMETHING</div></div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Finding all the elements in a list between two elements (not using index, and with wrap around)

shuffling all elements between given two elements

Delete all elements between two elements

wrap all elements with div

Select all elements between two known elements by CSS selector

How to select all elements between two same elements?

Is there a DOM function which deletes all elements between two elements?

Access all li elements between two specific heading elements

Get all list elements between two dates in python

select rows if their elements form all possible pairs between two columns

Get all ul elements in between two h3 tags

ruby/rails array all elements between two indices

generate all possibilities while swapping elements between two lists in scala

How to check whether all elements of array are in between two values

Get all arrays in between two elements in a multidimensional array PHP

How to wrap two elements in jQuery by respecting their parent?

Wrap items only if they are two certain elements

How to wrap two dynamically created elements with span?

Is there a way to dynamically wrap two elements into a div IF they exist?

How to wrap all children elements in a class?

How to wrap all elements in each container

wrap all elements after each specific tag

Is there a Gebish way of selecting elements between two elements?

Select elements between two defined elements

join list elements between two elements in a list

Find the elements between matching elements in two lists

Flexbox - How to split row 50/50 between two elements and wrap if width falls below min-width?

Do I need to wrap all my <input> elements in <form> elements?

Get all elements between element type (h1's) and wrap in div