Wrap a div around every 2 elements in jQuery

Jordash

I have some HTML like this:

<span>Header Here</span>
<span>Content Here</span>
<span>Another Header</span>
<span>Some Other Content Here</span>

As you can see the HTML isn't really formatted in a very accessible way, and unfortunately I can't change the output, what I need to do is wrap every 2 spans with a div so the output becomes this:

<div class="row">
    <span>Header Here</span>
    <span>Content Here</span>
</div>
<div class="row offsetRow">
    <span>Another Header</span>
    <span>Some Other Content Here</span>
</div>

The offsetRow class will repeat every other row.

Is this even possible in jQuery?

Arun P Johny

You can use .wrapAll() like

var $els = $('span');
for (var i = 0; i < $els.length; i += 2) {
  $els.slice(i, i + 2).wrapAll('<div class="row ' + (i > 0 && i % 2 == 0 && i % 4 != 0 ? 'offsetRow' : '') + '"></div>')
}
.row {
  color: grey
}
.offsetRow {
  background-color: lightblue;
}
span {
  border: 1px solid red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<span>Header Here</span>
<span>Content Here</span>
<span>Another Header</span>
<span>Another Header</span>
<span>Another Header</span>
<span>Another Header</span>
<span>Another Header</span>
<span>Another Header</span>

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 div around 2 sequential elements in jQuery

From Dev

JQuery, wrap div around every single div

From Dev

Wrap a div around every iframe

From Dev

Wrap a div around every iframe

From Dev

Wrap <a> around <div> without jQuery

From Dev

Wrap a div around every four divs

From Dev

Wrap a div around every 4 items in a loop

From Dev

Wrap elements into div with jQuery

From Dev

Adding a Div tag to wrap around LI elements

From Dev

Wrap around groups of elements using jQuery

From Dev

jQuery wrap a div around a massive block of HTML

From Dev

how wrap div around two element with jquery

From Dev

jQuery wrap a div around a massive block of HTML

From Dev

how wrap div around two element with jquery

From Dev

How to wrap a link around a div jQuery?

From Dev

Jquery wrap elements inside div

From Dev

Is it possible to wrap DIV´s around form-elements in web2py controller

From Dev

Wrap a div around every three divs using xslt

From Dev

Wrap class around 2 repeating classes in jQuery

From Dev

How can I wrap a div around existing elements?

From Dev

How to wrap every 2 elements inside a container in a div, except the ones with a certain class out?

From Dev

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

From Dev

Wrap multiple selected elements with a div with jquery

From Dev

Wrap div around a hidden div

From Dev

wrap div around other divs

From Dev

Wrap text around a div in html

From Dev

How to wrap div around image?

From Dev

jQuery .wrap() - specify position to wrap around

From Dev

jQuery wrap only around string