Hi! I’m trying to script/code a series of dropdowns, where if you select a certain item in a dropdown, it cost a certain amount of money.
For example:
Type Item Name Item Price
Dropdown - Motherboard
Selection1 - Motherboard1 - 5$
Selection2 - Motherboard2 - 10$
Dropdown - Graphics card
Selection1 - Graphics1 - 5$
Selection2 - Graphics2 - 10$
Motherboard2 + Graphics1
Total: 15$
I would like it so if a client selects motherboard2 and then Graphics1 it will add up to be 15.
I want that for all of my dropdowns but I can’t seem to find a clear way of how to do this. I am new, I have a bit of experience in LUA (thus my slight coding knowledge) but I’ve never used Javascript.
If anyone could help me it would be well appreciated! Thanks!
Au revoir
J.D
October 1, 2020, 11:29pm
2
You can try something like:
const options1 = [
{ label: "Motherboard1", value: 5 },
{ label: "Motherboard2", value: 10 }
];
const options2 = [
{ label: "Graphics1", value: 5 },
{ label: "Graphics2", value: 10 }
];
$w.onReady(function () {
$w("#dropdown1").options = options1.map(p => { p.value = p.value.toString(); return p; });
$w("#dropdown2").options = options2.map(p => { p.value = p.value.toString(); return p; });
const selectedArr = [];
$w("#dropdown1, #dropdown2").onChange(e => {
const t = e.target;
const selectedItem = { label: t.options.find(p => p.value === t.value).label, value: Number(t.value) };
selectedArr.push(selectedItem);
$w("#text1").text = `${selectedArr.map(p => p.label).join(" + ")}\nTotal: ${selectedArr.reduce((a,c) => a + c.value, 0)}$`;
});
});