Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

180
Views
How to Wrap Content Inside div by div pure js

I have the following code:

<div id="a"><p>this is p</p></div>

I want to wrap content inside like

<div id="a"><div class="wrap"><p>this is p</p></div></div>
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You can do:

const p = document.querySelector('p');
p.outerHTML = `<div class="wrap">${p.outerHTML}</div>`;
.wrap{border:2px solid blue}
#a{border:2px solid red}
<div id="a">
  <p>this is p</p>
</div>

about 4 years ago · Juan Pablo Isaza Report

0

let curDiv = document.getElementById("a");
curDiv.innerHTML = `<div id="wrap">${curDiv.innerHTML}</div>`
#a{
  border:1px solid green;
  padding: 6px;
}
#wrap{
  border:1px solid red;
  padding: 6px;
}
<html>

<body>
  <div id="a">
    <p>this is p</p>
  </div>
</body>

</html>

about 4 years ago · Juan Pablo Isaza Report

0

Use this:

var wrapDiv = document.createElement('div');
wrapDiv.setAttribute('class', 'wrap');

var children = document.getElementById('a').getElementsByTagName('p')[0];
children.parentNode.insertBefore(wrapDiv, children);
wrapDiv.appendChild(children);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!