Total of text Fields

Hello,

Is there a code that would automatically total the value of text fields without any user interference. I have text1, text2, & text3 that will populate numbers. I want text4 to show the total. So if those 3 texts boxes show 0 then the total will be zero and will calculate as numbers are uploaded into those text boxes.

Thank you in advance for any help.

https://www.w3schools.com/jsref/jsref_parseInt.asp
https://www.w3schools.com/jsref/jsref_parsefloat.asp

Can you elaborate more please. I don’t want a button to trigger any total. I want it to trigger automatically. Like in Excel how one cell is the Sum of others and will change/update if those cells values are changed.

Just typed long answer, hit my mouse, page refreshed and whole answer lost. Will try again tomorrow.

Giri you are not alone, it happens often to me, too. :sweat_smile::smile:

No worries, thank you

Any update with retyping that Answer?

Thanks,

@bensmitth

I have text1, text2, & text3 that will populate numbers
How does it look like. You should describe your situation a little bit better, if you want to get some answer.

The best way to describe something is a little picture of your example-project.
Anybody knows, what you see in front of your monitor and how your project is structured.

Hi there

You can get the total of fields like this, and the total will be automatically calculated.

const tax = $w('#tax').text;
const shipping= $w('#shipping').text;
const handling = $w('#handling ').text;

$w('#tax, #shipping, #handling').onInput(() => {
    $w('#total').text= Number(tax) + Number(shipping) + Number(handling);
})

Hope this helps~!
Ahmad

@russian-dima don’t be so hard on them, the question is clear, he wants to have the total of three text elements which represent numbers in his case, notice my answer.

@ahmadnasriya

No, i am not hard on them :grin:

But if you take a look on Giri’s answer, you will recognize, that there are a lot of ways how to interpret the question.

Yes i also thought in your way of thinking. But this was so simple, that i thought the post-opener was perhaps asking for some more.

So the result:
An exact answer, needs an exact question :grin:

But i think you are right, next time i won’t ask anymore :joy: (that could cause a desaster).

@russian-dima Nah, I don’t think it will be a disaster :joy: Keep asking

Hey Ahmad,

Thank you for getting back to me. The code you sent is very helpful. Except my text fields populate numbers after a filter/search is done, and not by input. So a drop down filters/searches through results and populates the numbers in the text fields that I want totaled.

I believe . onInput (() => { would need to be changed. Not sure to exactly what though.

Thanks,

@bensmitth You have the solution, you just need to know where you place it.
In this case, after filtering the dataset.

let filter = wixData.filter().eq('field', value);

$w('#dataset').setFilter(filter).then(() => {
    $w('#total').text= Number(tax) + Number(shipping) + Number(handling);
})