Then if they pick Feta, Cheddar and Salami it would then display a total of $6.00 in a text field.
It would be easy if I could assign the same value to each selection then just add them with code but you cant have the same value twice so I cant have two items at the same price.
I thought about doing this with a database by having a column with the pricing and then sum the price based on the selection and then display it in the field but I have no idea how to actually pull that off. I have searched all over the forum and am not sure where to start.
Ideally I would put these in a table of data to retrieve the prices but maybe you just need something a bit simpler here. You could do something like adding a unique key to the beginning of each value and then just remove it when doing the math like a2.00, b2.00, c1.50 then ignore that first character when summing. Not exactly elegant but it works.
export function checkboxGroup1_change ( event ) { let checkBoxValues = $w ( ‘#checkboxGroup1’ ). value let totalPrice = 0 for ( let i = 0 ; i < checkBoxValues . length ; i ++){
totalPrice = totalPrice + parseFloat ( checkBoxValues [i]. substring ( 1 ))
}
}
@archaeous I never even thought of that! You are genius. I will try it out and let you know how it goes! Thanks a million!
Ok so I tried it and it does work. Now I am just figuring out how to write that to an input. As I want to now display the totalPrice in an input field that they could submit with the form. I thought it would be something like this;
$w ( ‘#input4’ ). value = totalPrice ;
but I get the error saying;
Type ‘number’ is not assignable to type ‘string’. It’s recommended you use a new variable, such as “let newVar = …” to make sure that all code completion work correctly.
Thanks for you help and sorry I am not better at this.
@jordon There seems to be some IDE issues recently where even number type fields will give this error. It still should work when you run it even though the code gives an error. If it happens when you actually run it you can check to make sure the input is of type number. If it is you can always change the data type of a variable. So you could do something like
let totalStr = totalPrice.toString()
$w(‘#input4’).value = totalStr
keep in mind though that if the input is string typed if you need to do any math with it later you will have to change it back to a number.
@archaeous Ok, I have made some great progress. I ended up wrapping it in a function so I could do some math and add it together and put the total in the input field.
$w.onReady(function () {
$w("#checkboxGroup1").onChange(Price);
$w("#checkboxGroup2").onChange(Price);
});
function Price() {
let checkBoxValues = $w('#checkboxGroup1').value
let totalPrice = 0
for (let i = 0; i < checkBoxValues.length; i++){
totalPrice = totalPrice + parseFloat(checkBoxValues[i].substring(1))
}
let checkBoxValues2 = $w('#checkboxGroup2').value
let totalPrice2 = 0
for (let i = 0; i < checkBoxValues2.length; i++){
totalPrice2 = totalPrice2 + parseFloat(checkBoxValues2[i].substring(1))
}
$w('#input4').value = (totalPrice + totalPrice2) ;
}
This works great however I want to add a drop down that multiplies the total value by 2 or 4 or 6 depending on how many people are ordering this. I tried to simply do this:
However I get really weird numbers.
Ex, If they Select 2 People with the value set to 2 in the drop down.
Then select one of the ingredients that are $2.00 I get
2.0020
Not sure what I did wrong on this if I pick no ingredients and only a drop down value I get 2.000 or 4.000 or 6.000 and the other inputs become Cents and not dollars. Its like the decimal place moves on them.