jquery - Add wrapper after ever second div -


i'm trying add wrapper around ever second div include first , second divs

live example

js

$('.block').wrap('<div/>'); 

the desired effect result in this

html

<div><-- added js -->     <div class="block">         <div class="content">         block 1         </div><!--content end -->     </div><!--block end -->      <div class="block">         <div class="content">         block 2         </div><!--content end -->     </div><!--block end --> </div><-- added js -->  <div><-- added js -->     <div class="block">         <div class="content">         block 3         </div><!--content end -->     </div><!--block end -->      <div class="block">         <div class="content">         block 4         </div><!--content end -->     </div><!--block end --> </div><-- added js -->  <div><-- added js -->     <div class="block">         <div class="content">         block 5         </div><!--content end -->     </div><!--block end -->      <div class="block">         <div class="content">         block 6         </div><!--content end -->     </div><!--block end --> </div><-- added js --> 

select every other .block element, add next .block element 2 , 2 together, , wrapall div :

$('.block:even').each(function() {     $(this).add( $(this).next('.block') ).wrapall('<div />'); }); 

fiddle


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

keyboard - C++ GetAsyncKeyState alternative -

android - java.net.UnknownHostException(Unable to resolve host “URL”: No address associated with hostname) -