It works in my head but not in the real world… The checkbox codes works if I just show and hide elements i.e. if I connect the two fields via the database green icon to two separate text fields but I want to get it changing by drawing from the database to ease the setItem if used.
Many thanks in advance!
function privateCheckbox () {
let Price = $w("#dynamicDataset").getCurrentItem();
for (let i = 1; i < 3; i++) {
$w(`#checkbox${i}`).onChange(() => {
const checkBoxes = ['checkbox3', 'checkbox4'];
let filtered = checkBoxes.filter(item => item !== `checkbox${i}`);
filtered.forEach((checkbox) => {
$w(`#${checkbox}`).checked = false;
});
if ($w('#checkbox1').checked) {
$w('#pricePrivate1to1').text = Price.price11.toString();
}
if ($w('#checkbox2').checked) {
$w('#pricePrivate1to1').text = Price.privatePrice12.toString();
}
});
}
}
I would like to help but I didn’t understand what you were trying to accomplish even when recreating your code, can you please elaborate and try to explain again in more detail?
Thanks for getting back to me. I will try to explain my problem again in some more detail. From the picture below, I’m trying to achieve when the user checks, 1:1 it shows £400 for the course, if they select 1:2 it will show £460 (this element is actually hidden, but I have shown it here for reference. I have the code below which works.
function privateCheckbox () {
for (let i = 3; i < 5; i++) {
$w(`#checkbox${i}`).onChange(() => {
const checkBoxes = ['checkbox3', 'checkbox4'];
let filtered = checkBoxes.filter(item => item !== `checkbox${i}`);
filtered.forEach((checkbox) => {
$w(`#${checkbox}`).checked = false;
});
if ($w('#checkbox3').checked) {
$w('#pricePrivate1to1').show(), $w('#pricePrivate1to2').hide();
}
if ($w('#checkbox4').checked) {
$w('#pricePrivate1to1').hide(), $w('#pricePrivate1to2').show();
}
});
}
}
But I would like to extract the information straight from the database without using two separate boxes that are connected to the database through the ‘Connect to Data’ icon
The reason for this is to be able to setItem for the value of this text box ‘pricePrivate1to1’ to the payment screen. I hope this makes things a little more clear, but let me know if you need anything else to clarify.
Ok, I think I understand, first of all, I would use radio buttons instead of checkbox because you want only one choice, am I right?
second, you can use an onChange on the radio button group, get the chosen value, and use a wixData.query() function to get the items while refining the query with the chosen value using the eq() function.
Like so:
Thank for the advice, and your correct Radio button would be easier, i just had the checkbox code in another part of the website so tried to transfer.
The code you have sent over I can’t seem to grasp how to get the price for the course that is on the dynamic page. I will try and explain a little more of what is working to populate the fields, I’m just struggling to get them to toggle when the other box is ‘checked’