Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wrap two divs in a container

Tags:

html

jquery

I have a container which contains multiple divs, of which i need to wrap two of them in another div, using jquery. Please suggest the possible way.

actual structure

<div class='parent'>
 <div class='a'>abcd</div>
 <div class='b'>abcd</div>
 <div class='c'>abcd</div>
 <div class='d'>abcd</div>
</div>

expected structure:

<div class='parent'>
 <div class='a'>abcd</div>
 <div class='child'>
  <div class='b'>abcd</div>
  <div class='c'>abcd</div>
 </div>
 <div class='d'>abcd</div>
</div>
like image 290
Green Wizard Avatar asked Jun 10 '14 07:06

Green Wizard


People also ask

How do you wrap two divs?

use Jquery . wrapAll() - It wraps the html structure around all the matched elements.


2 Answers

You can use wrapAll() function! It wraps all the elements that match the selector! :)

$('.b,.c').wrapAll('<div class="child"></div>');

JSFIDDLE DEMO

like image 93
skshoyeb Avatar answered Oct 19 '22 23:10

skshoyeb


Use wrapAll method:

$('.b, .c').wrapAll('<div class="child" />');

inspect this demo

like image 44
Bhojendra Rauniyar Avatar answered Oct 20 '22 00:10

Bhojendra Rauniyar