Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

case statement for States

Avatar

Level 3

Hi All,

I am trying to populate a text field based on what the user chooses in a drop down that contains the 50 United States of America.

So...let's say for now that I want the text field to say "Birmingham" if the user chooses "Alabama".

The only thing I have so far is totally wrong:

function setText()

{

var statechoice = State.value;

switch (statechoice) {

case 1:

Text1.rawValue = "Filing is required in Alabama";

break

case 2:

Text1.rawValue = "Very cold and Filing is required in Alaska";

break

default:

Text1.rawValue = "Choose a state please";

break

}

Does it always have to be case 1, 2, 3 and so on? Can't it ever be "case (something is true and something equals 7):"?  I mean, do I always have to use integers to distinguish the cases?
later on, I want to populate that drop down based on several choices that the user makes. I want to have a list of conditionals where each conditional expresses a unique combination of choices that the user has made.
Do you like cold food? Yes
Lactose intolerant? No
Text Field says "Have an ice cream cone for dessert".
How do I get integer values from a drop down? dropdown.value? dropdown.index? dropdown.selectedIndex?
How do I combine these choices in the case statements? case (dd1 = 1 and dd2 = 4 and dd3 = 2):   ???
Sorry for this discursive question,
I just need a good code example to follow.
Many Thanks,
Joe

1 Accepted Solution

Avatar

Correct answer by
Level 10

You can try some thing like below..

Otherwise, Switch statement evaluates only to the value that is passed next to the switch command..

var statechoice = State.rawValue;
switch (statechoice) {
case "AL":
  Text1.rawValue = "Filing is required in Alabama";
  Text1.rawValue = Text1.rawValue + setText();
  break;
case "AK":
  Text1.rawValue = "Very cold and Filing is required in Alaska";
  break;
default:
  Text1.rawValue = "Choose a state please";
  break;
}

//Added a function inside the same event and calling this function in the above switch case statements.
function setText(){
switch (otherDropDown.rawValue) {
case "Yes":
  return "Yes Selected";
  break;
default:
  return "No Selected";
  break;
}
}

Hope this helps..

Thanks

Srini

View solution in original post

3 Replies

Avatar

Level 10

Few things to mention.

1) You can get the selected value from the dropdown by using rawValue property.. But in your code you have used "State.value"..It should be "State.rawValue"..

2) In the Case statement:

       case 1/2/3 etc.. are values of the selected value from the State dropdown. If you assign "AL" for Alabama, then you need to use "case "AL":"

     

function setText()

{

var statechoice = State.rawValue;

switch (statechoice) {

case "AL":

Text1.rawValue = "Filing is required in Alabama";

break;

case "AK":

Text1.rawValue = "Very cold and Filing is required in Alaska";

break;

default:

Text1.rawValue = "Choose a state please";

break;

}

Find my changes to your script above..

Thanks

Srini

Avatar

Level 3

HI Srini,

Yes...correct..excellent...thanks. This works.

And, is there any way for the case condition to evaluate more than one thing?

What if I wanted to select the code to execute based on the fact that the user selected Alaska AND he selected YES or NO in another drop down?

Would that be:

case ("AK" & othervariable=="YES"):

Or does each case only evaluate a single condition?

Thanks,

Joe

Avatar

Correct answer by
Level 10

You can try some thing like below..

Otherwise, Switch statement evaluates only to the value that is passed next to the switch command..

var statechoice = State.rawValue;
switch (statechoice) {
case "AL":
  Text1.rawValue = "Filing is required in Alabama";
  Text1.rawValue = Text1.rawValue + setText();
  break;
case "AK":
  Text1.rawValue = "Very cold and Filing is required in Alaska";
  break;
default:
  Text1.rawValue = "Choose a state please";
  break;
}

//Added a function inside the same event and calling this function in the above switch case statements.
function setText(){
switch (otherDropDown.rawValue) {
case "Yes":
  return "Yes Selected";
  break;
default:
  return "No Selected";
  break;
}
}

Hope this helps..

Thanks

Srini