Math using CMS fields

side question: Can you also put the sum with punctuation? so 1000 » 1.000 and 1000000 » 1.000.000 ?

You need to replace the cars = [] of your old code snipet by the new one, there shouldn’t be two times cars = [] in your code. As for the <p>total: <span id="target"></span></p> you can simly inject it into a webflow custom code embed component within your layout. The '...' or quotes wrapping your numbers in your javascript have no effect because in the script I wrote, I wrapped the a and b values within the Number() method which coherce strings into numbers. If the quotes would have been an issue, you’d see a NaN (Not a Number) value into your target span. As for your formating question, sure it’s possible to format the numbers with punctuation, that is one or two javascript lines.

1 Like

If i replace the cars = [] the script doesn’t run and the google map doesn’t get populated.

then ‘cars’ is not defined devtools reports:
image

Update!
i removed cars = [], from your snippet and kept the original! Now it’s seems to WORK!!!

image

image

Super awesome!!!
Now i only need to get the punctuation?
Do you perhaps have any example(s) to do this?

Cool, I’m happy it worked :wink:

1 Like

yes, I did a similar codepen a few month ago.

wow that’s a bit more than two lines.
I’ve googled a bit and found this:

function numberWithCommas(x) {
    return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}

could it work with this as well?

you can try :slight_smile:

I’ve gotten it to work with:

Last question - how can i ‘count’ the total length (amount of items) of the array(s) and inject them to some span as well?

You can use the .length method to return the amount of items in an array.

1 Like

Is it possible to use this inside a collection list? So far I am failing. As far as I understand you can’t have the same id tag on multiple items on the same page. And I haven’t found a way to give separate id tags to items in a collection list. (This could just be my ignorance) If I had an id tag to a div in a collection field Webflow gives it to the duplicate divs in the collection. Not sure if there is a work around for this or I need to use a different JavaScript to get what I want.
Thanks,

Hello Anthony,
you are a real life saver!! I have been dealing with the same problem since quite a while but never got a proper solution. Two quick questions:

  1. Can I perform more advanced operations such as percent (eg. A-((A/100)*B) ) where A is the gross number from a CMS field and B is the percentage from a CMS field?
  2. How do I place the final result on my page (eg. 'Final Price = A-((A/100)*B) )?
    Wish you can still reply to this tread.
    Cheers
    Valter

Thank you for solution @anthonysalamin and the read-only link @icexuick

I was able to get this working with with Jetboost favoriting if that helps anyone.

Jetboost updates a number field in your collection item on click, and then your solution shows the total number from the whole collection after refreshing page. I was hoping to have the total show up automatically but this was close enough.