Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

131
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda