Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

189
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda