Wrapping multiple elements (jQuery)

I have this part of HTML:

div.content
  div.one
    content
  div.two
    content
  div.three
    content

I want to add two divs above and below and wrap one div around it so that it becomes:

div.top
div.wrapper
  div.content
    div.one
      content
    div.two
      content
    div.three
      content
div.bottom

I know several package selectors (innerWrap, wrapAll, etc.), but I'm not sure how to wrap 2 divs.

The following jQuery may work, but is there a better way to write it?

$('content').wrap('<div class="wrapper"></div');
$('.wrapper').before('<div class="top"></div>');
$('.wrapper').after('<div class="bottom"></div>');
+3
source share
1 answer

I would do this on one line to minimize the search:

$('content')
     .before('<div class="top"></div>')
     .after('<div class="bottom"></div>')
     .wrap('<div class="wrapper"></div>');
+5
source

Source: https://habr.com/ru/post/1724822/


All Articles