You can use the wrap() method:
Wrap the HTML structure around each element in the set of matched elements.
$('.C').wrap('<div class="A"><div class="B"></div></div>');
Fiddle
Note that jQuery('</div></div>').insertAfter('.C'); does not generate html tags.
My answer has one downstream signal from an anonymous downvoter, of course, because I used
$('.C').wrap('<div class="A"/>') , and the commentator also said that it is broken. Here is the
jQuery documention part:
Consider the following HTML:
<div class="container"> <div class="inner">Hello</div> <div class="inner">Goodbye</div> </div>
Using .wrap() , we can insert an HTML structure around the internal <div> elements, for example:
$('.inner').wrap('<div class="new" />');
A new <div> element is created on the fly and added to the DOM. The result is a new <div> wrapped around each matched element:
<div class="container"> <div class="new"> <div class="inner">Hello</div> </div> <div class="new"> <div class="inner">Goodbye</div> </div> </div>
source share