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

130
Vistas
loop elements in array jQuery

Sorry for my bad english but,

I'm trying to loop all children elements in array but it keeps looping only the first element however how much elements inside the parent

$(".js-drawer-open-cart").on("click", function (event) {
  var items = [];

  $(".cart__items .cart__item").each(function () {
    let product_title = $(".cart__item--name").attr("data-product-title");
    let product_id = $(".cart__item--name").attr("data-product-id");
    let product_variant = $(".cart__item--name").attr("data-product-variant");
    let product_quantity = $(".cart__item--name").attr("data-product-quantity");
    let product_price = $(".cart__item--name").attr("data-product-price");

    dataLayer.push({ ecommerce: null }); // Clear the previous ecommerce object.
    var item = {};

    item.item_name = product_title;
    item.price = product_price;
    item.item_brand = "El grande Porto";
    item.item_variant = product_variant;
    item.quantity = product_quantity;

    
    items.push(item);
  
  });

  dataLayer.push({
    event: "view_cart",
    ecommerce: {
      items: items,
    },
  });

});
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

It works for me thanks to epascarello's answer :

All i had to do is this :

$(this).find(".cart__item--name").attr("data-product-title");
about 4 years ago · Juan Pablo Isaza Denunciar

0

$(".cart__items .cart__item").each iterates over all the elements that match the given selector, not the children of that element. If there should only be one match, try taking the first element and looping over its children?

Example: $(".cart__items .cart__item").first().children().each

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