Although this is a form created in Breezingforms for Joomla, my question is more of a generic javascript question. I am very new to javascript so hopefully someone can answer this easily. This is being called upon initialization of a form.
I am using a javascript loop in a selection list object to hide/unhide various sections of a form, depending on the selection list choice. I’m trying to make the if/else if statements less clumsy. I’m sure there is a way to make this shorter and more concise, but being such a novice, I haven’t been able to figure it out.
On a side note (not as important at the moment, but it will be), I’d like to use this script for other selection list calls, with minor variations. I still am not too clear on reusing code. If someone has any comments on that, it would be appreciated.
BTW, I am even more of a novice at jQuery, so if you go there, be VERY kind! 🙂
var selListVal = "";
var selListInput = JQuery("[name=\"mySelectionList[]\"]");
for (var i = 0; i < selListInput.length; i++)
if (selListInput[i].value) {
if (selListVal != "") selListVal += ",";
selListVal += selListInput[i].value;
}
if( selListVal == "myselection01" ){
ToggleFields('on', 'section', 'mysection01A', DeactivateField);
ToggleFields('on', 'section', 'mysection01B', DeactivateField);
}
else if( selListVal == "myselection02" ){
ToggleFields('on', 'section', 'mysection01A', DeactivateField);
ToggleFields('on', 'section', 'mysection01B', DeactivateField);
ToggleFields('on', 'section', 'mysection02A', DeactivateField);
ToggleFields('on', 'section', 'mysection02B', DeactivateField);
}
else if( selListVal == "myselection03" ){
ToggleFields('on', 'section', 'mysection01A', DeactivateField);
ToggleFields('on', 'section', 'mysection01B', DeactivateField);
ToggleFields('on', 'section', 'mysection02A', DeactivateField);
ToggleFields('on', 'section', 'mysection02B', DeactivateField);
ToggleFields('on', 'section', 'mysection03A', DeactivateField);
ToggleFields('on', 'section', 'mysection03B', DeactivateField);
}
else {
ToggleFields('off', 'section', 'mysection01A', DeactivateField);
ToggleFields('off', 'section', 'mysection01B', DeactivateField);
ToggleFields('off', 'section', 'mysection02A', DeactivateField);
ToggleFields('off', 'section', 'mysection02B', DeactivateField);
ToggleFields('off', 'section', 'mysection03A', DeactivateField);
ToggleFields('off', 'section', 'mysection03B', DeactivateField);
}
How about something like this?
or to illustrate the idea by
user968951