Calculating two export functions

Hey Guys,

Im fairly new to coding, so please bear with me. I have two Inputs on my site, I want the user to be able to enter a value into both and the #total.text will add them together. I have got as far as the below, which works, but one replaces the other rather than them being added. Can someone please help!? Thanks

export function input1_change_1(event, $w) {

let price = Number(449);

let selectedQuantity = Number(event.target.value);

$w('#total').text = String(850+ (selectedQuantity * price)); 

}

export function input2_change_1(event, $w) {

let price = Number(39);

let selectedQuantity = Number(event.target.value);

$w('#total').text = String(selectedQuantity * price); 

}

Do you mean something like;

$w.onReady(() => {
    let price1 = 0;
    let price2 = 0;
    $w("#input1, #input2").onChange(event => {
        let target = event.target.id;
        let newVal  = Number($w("#" + target).value);
        target === "input1" ? price1 = 449 *  newVal  : price2 = 39 * newVal;
        $w("#total").text = (price1 + price2).toLocaleString();
    })
})

Works perfectly! Thanks

You’re welcome :slight_smile:

Hey, how would I add more inputs to the above code?

You can try:

let prices = [
{
elementId:" input1",
pricePerUnit : 449
},
{
elementId: "input2",
pricePerUnit : 39
},
{
elementId: "input3",
pricePerUnit : 70
}
//etc...
];
prices = prices.map(e => {e.qt = 0; return e;});
let selector = prices.reduce((a,c) => a + `#${c.elementId},`, "");
$w.onReady(() => {
$w(selector).onChange(event => {
let itemToUpdate = prices.find(e => e.elementId === event.target.id); 
itemToUpdate.qt = Number($w("#" + event.target.id).value); 
let total = prices.reduce((a,c) => a + c.pricePerUnit  * c.qt, 0);
$w("#total").text = total.toLocaleString();
})
})

Thanks!

I can’t seem to make that work. I wonder if I lay out what calculation I am trying to do, that might help?

What I need to do is:

Input 1: Value * 1 = X1
Input 2: Value * 0.02 =X2
Input 3: Value * 1 = X3
Input 4: Value * 0.04 =X4

X1X2 = Y1
X3
X4 = Y2

Y1+Y2 = Y3

then,

Input 1: Value * 1 = X5
Input 2: Value * 0.14 =X6
Input 3: Value * 1 = X7
Input 4: Value * 0.15 =X8

X5X6 = Y4
X7
X8 = Y5

Y4+Y5 = Y6

Total = Y6-Y3

This is the input form, if it helps to visualise…

Really appreciate your help!
Jon