Dropdown and Free text Option

Adam,

When I copied the code into the editor and added the appropriately named elements, there is this error which means that you are not properly “closing off” an if statement.

When there are more than two possibilities that you have to handle, Javascript has the switch statement . It’s a cleaner way to handle the various options. This is how how it would be used with the code you have so far:

$w.onReady(function() {
let placeholderStr = "", placeholdText = "";
$w('#subPrefix').options = Mr;
$w('#prefix').onChange(() => {
switch ($w('#prefix').value) {
case 'Mr':
$w('#subPrefix').disable();
$w("#subPrefix").value = null;
$w("#actual").value = null;
placeholderStr = $w("#subPrefix").placeholder = "Option Disabled"; // "Enter name"
placeholdText = $w("#actual").placeholder = "Option Disabled"; // "Enter name"
break;
case 'Mrs':
$w('#subPrefix').disable();
$w("#subPrefix").value = null;
$w("#actual").value = null;
placeholderStr = $w("#subPrefix").placeholder = "Option Disabled"; // "Enter name"
placeholdText = $w("#actual").placeholder = "Option Disabled"; // "Enter name"
break;
case 'Military Officer':
$w('#subPrefix').options = MilitaryOfficer;
$w('#subPrefix').placeholder = 'A Prefix for a Military Officer';
placeholdText = $w("#actual").placeholder = "Option Disabled"; // "Enter name"
$w('#subPrefix').enable();
$w("#actual").value = null;
$w("#prefix").value = null;
break;
default:
$w('#actual').expand();
$w('#actual').options = AddmyOwnPrefix;
$w('#prefix').disable();
$w('#subPrefix').disable();
$w("#prefix").value = null;
$w("#subPrefix").value = null;
break;
}
})
});

Also, your code suggests that you have MilitaryOfficer and AddmyOwnPrefix arrays in memory and that they are going to be used to populate the subPrefix dropdown. I didn’t see in your code where you defined these arrays. To properly assign values to a dropdown in code, you have to construct the array like the documentation suggests:

Anthony thanks for this. i’m going to look at this closely tomorrow morning and implement your changes. Will you be around tomorrow to view any feedback?
Thanks
Adam

also this is a small excerpt from the js file

and one last thing, in the code you sent me, you refer to #actual. I have removed #actual altogether the only options for dropdown are #prefix dd1, #subPrefix dd2 and input field (#addOwnPrefix) is this correct still based on your code for me

Thanks
Adam

I will be preoccupied the next several days and may be very slow in getting back to you.

Hi Anthony, i’ve nailed this one. Essentially the else if ($w(‘#prefix’).value === ‘Add my Own Prefix’) {
was just slightly different from what i had entered. Hence the code for expand or enable or anything would not work correctly.
Thanks also for reminding me about using console.log i think this also helps fault find the solution.
Cheers
Adam