0

Here is a fiddle of what I have so far. I want to wrap all groups of 'OK' starting classes into their own:

<div class="wrapper">

I figured one possibility could be to take the first element of each group, then run a nextUntil() until it finds element with class not matching the class, then .wrapAll().

It should in the en look like this:

<div class="foo">aaa</div>
<div class="wrapper">
  <div class="ok-4">bbb</div>
  <div class="ok-21887">ccc</div>
  <div class="ok-6">ddd</div>
</div>
<div class="bar">eee</div>
<div class="baz">fff</div>
<div class="wrapper">
  <div class="ok-5a4">ggg</div>
  <div class="ok-12">hhh</div>
</div>
<div class="bim">iii</div>
double-beep
  • 5,031
  • 17
  • 33
  • 41
Nirs
  • 45
  • 8

2 Answers2

1

To wrap every individual div you can use .wrap():

$('div[class^="ok"]').wrap("<div class='wrapper'></div>");

Working Demo

Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125
0

Try this:

$('div[class^="ok"]').wrapAll("<div class='wrapper'></div>");

Updated Fiddle

Felix
  • 37,892
  • 8
  • 43
  • 55