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

193
Vistas
How to add a 'data-*' attribute when using Object.assign to create an html element

I have the following code:

let item = Object.assign(document.createElement('img'), {
    className: 'carouselItem',
    src: `/public/leerlingen/${data.item}`,
    loading: 'eager',
});
item.setAttribute('data-showtime', data.duration)

I want to set a data attribute in the object assign, instead of separately. I tried dataset: { showtime: data.duration }, but that results in the following typeerror TypeError: setting getter-only property "dataset"

Thanks in advance!

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

0

Maybe this might be a nicer way to do this, if all you want is to set attributes to the image element.

const imageRef = document.createElement('img');

Object.entries({
    className: 'carouselItem',
    src: `/public/leerlingen/${data.item}`,
    loading: 'eager',
    'data-showtime': data.duration
}).forEach(([key, value]) => {
    imageRef.setAttribute(key, value);
});
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