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

125
Views
Im having problems to retrieve the data attributes

I'm trying to retrieve the data attributes from the cart but it's not working.

I also would like to know if there's a way to shorten the code.

HTML:

<div id="product-listing" class="hidden">
  <div>
    <img src="img1.jpg" />
    <div class="description">Lagavulin 16 Year Old</div>
    <button class="add-to-cart"
            data-product-id="1"
            data-product-title="Lagavulin 16 Year Old" data-category="Scotch">
            Add To Cart
    </button>

Vanilla js:

function onPageLoad(event) {
  var title = document.getElementsByTagName("title")[0].innerText;
  console.log({
    event_name: "page loaded",
    page_title: title,

  });
}

var photo = document
  .getElementsByClassName("add-to-cart")
  .getAttribute("data-product-id");

for (var i = 0; i < photo.length; i++) {
  photo[i].onclick = function() {
    console.log({
      event_name: "add_to_cart",
      product_id: data-product-id,
      product_title: data-product-title,
    });
  }
};
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

This should work:

Array.from(document.getElementsByClassName("add-to-cart")).forEach(x => x.onclick = () => console.log({
  event_name: "add_to_cart",
  product_id: x.getAttribute("data-product-id"),
  product_title: x.getAttribute("data-product-title")
}));
<button class="add-to-cart" data-product-id="1" data-product-title="Lagavulin 16 Year Old" data-category="Scotch">Add To Cart</button>

<button class="add-to-cart" data-product-id="2" data-product-title="Some other Scotch" data-category="Scotch">Add To Cart</button>

You were not properly iterating over the elements returned by getElementsByClass and also not using element.getAttribute("") correctly.

To make it shorter I used streams.

about 4 years ago · Juan Pablo Isaza Report

0

Try this way:

var photo = document
  .getElementsByClassName("add-to-cart");

for (var i = 0; i < photo.length; i++) {
  photo[i].onclick = function() {

    console.log({
      event_name: "add_to_cart",
      product_id: photo[i].getAttribute("data-product-id"),
      product_title:  photo[i].getAttribute("data-product-title"),
    });
  }
};
about 4 years ago · Juan Pablo Isaza Report

0

Try this

var photo = document.querySelectorAll('.add-to-cart');
photo.forEach(function(button){
    button.addEventListener('click',function(){
        console.log(this.getAttribute('data-product-id'));
    },{passive:true});
});
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!