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

183
Views
Create div element in JavaScript and add as parent of another div which was already exist in html file using pure vanilla JavaScript

Right now my .html file like this:

<body>
  <h2 id="h2">Child</h2>
</body>

Here I want to make a parent div Element of #h2. Here I cannot replace body element... I want to create a parent div of h2 element like:-

I want my html file like this using vanilla JavaScript.

<body>
  <div id="parent">
    <h2 id="h2">Child</h2>
  </div>
</body>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Something like this should do the trick: create the parent element, append it in the body, and move (append) the h2 into the parent element.

var parentEl = document.createElement("div")
parentEl.setAttribute("id", "parent")

document.body.appendChild(parentEl)
parentEl.append(document.getElementById("h2"))
<body>
  <h2 id="h2">Child</h2>
</body>

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!