Search code examples
javascriptarrayswebflow

Get the value of multiple items with the same class


I want to get the value of multiple paragraphs with the same Class, but different content (numbers).

In detail these paragraphs contain numbers which I want to build a total and output it into another paragraph. The value of the paragraph is delivered by the CMS (Webflow). I can't work with native var in the script.

So I guess I have to build an Array and get the total. + Out put it with .innerHTML.

I'm new to JavaScript.

<p class="mwst-price" id="mwst-price"></p>

<script type="text/javascript" src="https://code.jquery.com/jquery-1.7.1.min.js"></script>
<script>
    var tax = {totalPrice}+sums*0.19;
    document.getElementById("mwst-price").innerHTML = tax;
</script>

Solution

  • let totalPrice = 0;
    document.querySelectorAll('p.myClass').forEach((paragraph) => {
      totalPrice += parseInt(paragraph.innerText);
    });
    
    const tax = totalPrice+sums*0.19;
    document.querySelector('#mwst-price').innerHTML = tax;