Custom code for finding sum of CMS items field

Hi.

I am trying to create a simple function that updates a divs innerHTML based on a sum I am pulling from a CMS collection.

It is currently set up like so:

<script>
  const items = document.getElementsByClassName("amount-count");
  let sum = 0;

  for (item of items) {
    sum += parseInt(item.innerHTML);
  }
  
  const total = document.getElementsByClassName("amount-total");
  total.innerHTML = "$" + sum;
  
</script>

If I console log “total”, I get what I’m looking for. But the page itself is not displaying the information at all.


Here is my site Read-Only: https://preview.webflow.com/preview/cpr-mro?utm_medium=preview_link&utm_source=dashboard&utm_content=cpr-mro&preview=fce14146ce8e01608eb83a0e87920a17&workflow=preview

Look for consumer > dashboard

total is an array in your code.

1 Like

So how do I resolve my issue then?

See accessing array elements on MDN Array - JavaScript | MDN

1 Like

Got it. Thank you for the help!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.