I have p
tags inside div
element.
I want to wrap the div
children/child with div
tag using cheerio
library.
I try to use wrap
: $('.parent').children().wrap('<div></div>');
But it wraps every element in my div
.
Is it possible to wrap all the children with one tag div
like this:
<div >
<div>
<p>Child 1</p>
<p>Child 2</p>
<p>Child 3</p>
</div>
</div>
The code I try to do:
import cheerio from 'cheerio';
const html = `
<div >
<p>Child 1</p>
<p>Child 2</p>
<p>Child 3</p>
</div>
`;
const $ = cheerio.load(html);
$('.parent').children().wrap('<div></div>');
console.log($.html());
// Output:
// <div >
// <div><p>Child 1</p></div>
// <div><p>Child 2</p></div>
// <div><p>Child 3</p></div>
// </div>
CodePudding user response:
Use method wrapInner
like that:
$('.parent').wrapInner("<div></div>");