Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

132
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!