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

98
Vistas
Adding a <div class></div> that encloses other elements with JavaScript

I'm editing a rather primitive forum, and it won't let me modify the HTML. I can only edit the CSS. So, I want to see if it would be possible to add a <div class""></div> in the HTML with JavaScript that encloses more of the existing elements.

The current HTML is as follows:

<div class="details">
  <div class="avatar">
    <img src="https://i.imgur.com/1r7oGBt.png"></div>
  <p><span class="u_title">None</span>
    <span class="u_rank"></span></p>
  <dl class="u_group"><dt>Group</dt>
    <dd>User</dd>
  </dl>
  <dl class="u_posts"><dt>Messages</dt>
    <dd>52</dd>
  </dl>
  <dl class="u_scoresystem rep_zero"><dt>Score System</dt>
    <dd>0</dd>
  </dl>
  <dl class="u_status"><dt>Estado</dt>
    <dd>Anónimo</dd>
  </dl>
</div>

And I would like it to be something like this, i.e. using <div class="overlay"></div>:

<div class="details">
  <div class="avatar">
    <img src="https://i.imgur.com/1r7oGBt.png"></div>
  <div class="overlay">
    <p><span class="u_title">None</span>
      <span class="u_rank"></span></p>
    <dl class="u_group"><dt>Group</dt>
      <dd>User</dd>
    </dl>
    <dl class="u_posts"><dt>Messages</dt>
      <dd>52</dd>
    </dl>
    <dl class="u_scoresystem rep_zero"><dt>Score System</dt>
      <dd>0</dd>
    </dl>
    <dl class="u_status"><dt>Estado</dt>
      <dd>Anónimo</dd>
    </dl>
  </div>
</div>

Is this possible?

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

That's easy:

  1. Iterate over all elements with class details.
  2. For each of those elements, destructure the children beyond the first child into a variable.
  3. Create the surrounding element using document.createElement.
  4. Append the surrounding element.
  5. Append the elements you restructured in 2. to the new element.

Don't try to solve the problem by copying some HTML strings and using innerHTML as that will lose you any event listeners potentially attached to the elements you wrap.

const details = document.querySelectorAll('.details');

for (const detail of details) {
  const [ first, ...childrenToWrap ] = [...detail.children];
  const div = document.createElement('div');
  div.className = 'overlay';
  detail.append(div);
  div.append(...childrenToWrap);
}
.overlay {
  background-color: orange;
}
<div class="details">
  <div class="avatar">
    <img src="https://i.imgur.com/1r7oGBt.png"></div>
  <p><span class="u_title">None</span>
    <span class="u_rank"></span></p>
  <dl class="u_group"><dt>Group</dt>
    <dd>User</dd>
  </dl>
  <dl class="u_posts"><dt>Messages</dt>
    <dd>52</dd>
  </dl>
  <dl class="u_scoresystem rep_zero"><dt>Score System</dt>
    <dd>0</dd>
  </dl>
  <dl class="u_status"><dt>Estado</dt>
    <dd>Anónimo</dd>
  </dl>
</div>

about 4 years ago · Juan Pablo Isaza Denunciar

0

It is very easy. Use document.createElement to create the element, add attributes, and append it to the selected element.

var myEl = document.createElement("div");

// Use "className" because JavaScript "class" is a reserved keyword.
myEl.className = "myClass";

// Append it
document.querySelector("div").appendChild(myEl);

Working live example:

var myEl = document.createElement("div");

// Use "className" because JavaScript "class" is a reserved keyword.
myEl.className = "myClass";

// Append it
document.querySelector("div").appendChild(myEl);

if (!!myEl) {
  myEl.textContent = "You can append other attributes too!";
}
<div>

</div>

You can add it as a parent element too:

var currentEl = document.querySelector("div");
currentEl.outerHTML = `<div class="myClass">${currentEl.outerHTML}</div>`;

You can append many other attributes too.

Read more about createElement at Document.createElement().

about 4 years ago · Juan Pablo Isaza Denunciar

0

I think you need something like this:

let messages = 52;
let score = 0;
document.body.insertAdjacentHTML('afterbegin',`
  <div class="details">
    <div class="avatar">
    <img src="https://i.imgur.com/1r7oGBt.png"></div><div class="overlay">
    <p><span class="u_title">None</span>
    <span class="u_rank"></span></p>
    <dl class="u_group"><dt>Group</dt><dd>User</dd></dl>
    <dl class="u_posts"><dt>Messages</dt><dd>${messages}</dd></dl>
    <dl class="u_scoresystem rep_zero"><dt>Score System</dt><dd>${score}</dd></dl>
    <dl class="u_status"><dt>Estado</dt><dd>Anónimo</dd></dl></div></div>
`);

In the above code, as you can see, you can insert messages and score dynamically to the string and also you can add the HTML dynamically using JavaScript to the body of your html document.

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