Hello. I’m trying to select multiple checkboxes, concatenate their values and store in a field in the database.
Following is a snippet of code
let allValues = ‘’;
$w(‘Checkbox’).forEach(checkbox => {
allValues += checkbox.value;
});
Tried this code but it shows an error saying ‘forEach’ does not exist on ‘Checkbox’. Does anyone know why and how to fix it?
Any help will be much appreciated, thanks.
What I normally do in such a case is name the check boxes with numbers
E.g.
Checkbox1
Checkbox2
Then, in code do an external for loop
for (let i=0; i<5; i++) {
$w(“checkbox”+i)…
}
@yoav-wix
Is the rest of the code to be kept the same then? As in,
for(let i=1;i<8;i++) {
$w(“#checkbox”+i).forEach(checkbox => {
allValues += checkbox.value
});
}
Forgive me but I’m quite new to wix. Also, how do I load the end result into my database? I need to load the concatenated result into “technologies” column in my database.
@kiransbharadwaj you don’t need to use .forEach
What I understand from you that you want to check each checkbox and add its value in allValues.
let allValues = ‘’;
// make a loop for 5 checkboxes(as many checkboxes you have)
for (let i=0; i<5; i++) {
// you need to check if this checkbox is checked or not.
if($w(“#checkbox”+i).checked){
// this check box is checked
allValues += $w(“#checkbox”+i).value;
}
}
be sure that your id for the checkboxes like this: “checkbox1”, “checkbox2”, “checkbox3”, “checkbox4”, “checkbox5”,
good luck.