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

209
Vistas
How to append elements to all items in an array

I'm trying to add a delete button to all items with a specific class in a menu but when I try to loop through the array of classes and append the button it can only be added to one of the items, anyone know how I can fix it?

let menuItems = document.getElementsByClassName('menu-item');
let deleteButton = document.getElementById('deleteButton');

for (var i = 0; i < menuItems.length; i++) {
  menuItems[i].append(deleteButton);
};
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You need to clone that element, and please don't use an ID, otherwise you'll end up having duplicated IDs.

const ELS_menuItems = document.querySelectorAll('.menu-item');
const EL_deleteButton = document.querySelector('#deleteButton');

ELS_menuItems.forEach(EL_item => EL_item.append(EL_deleteButton.cloneNode(true)));  
// Be advised that you'll end up having duplicated IDs in your DOM now
  • https://developer.mozilla.org/en-US/docs/Web/API/NodeList/forEach
  • https://developer.mozilla.org/en-US/docs/Web/API/Node/cloneNode
  • https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll
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