Expand my Community achievements bar.

What is wrong with this javascript?

Avatar

Former Community Member
This script is used to alter one drop down box based on the data from the first drop down.



It worked earlier today, but then I made changes and saved it, and now it doesn't work. I think I undid all my changes, but I must be missing something obvious because it still doesn't work. Any idea what's wrong with this javascript?



if (this.rawValue == 0) {//SQ1

DropDownList2.clearItems();

DropDownList2.addItem("Single Occ", "0");

DropDownList2.addItem("Double Occ", "1");



} else (this.rawValue == 1) {//SD2

DropDownList2.clearItems();

DropDownList2.addItem("Single Occ", "0");

DropDownList2.addItem("Double Occ", "1");

DropDownList2.addItem("Triple Occ", "2");

DropDownList2.addItem("Quad Occ", "3");

}
2 Replies

Avatar

Former Community Member
Alan, try adding an 'if' after your else. (should be else if)

Avatar

Level 7
You could also use 2 "if" statements or the "switch" statement.

// 2 if statements:

if (this.rawValue == 0) {//SQ1

DropDownList2.clearItems();

DropDownList2.addItem("Single Occ", "0");

DropDownList2.addItem("Double Occ", "1");

}

if (this.rawValue == 1) {//SD2

DropDownList2.clearItems();

DropDownList2.addItem("Single Occ", "0");

DropDownList2.addItem("Double Occ", "1");

DropDownList2.addItem("Triple Occ", "2");

DropDownList2.addItem("Quad Occ", "3");

}



The "switch" statement:



switch( Str(this.rawValue) ) {

case "0": {//SQ1

DropDownList2.clearItems();

DropDownList2.addItem("Single Occ", "0");

DropDownList2.addItem("Double Occ", "1");

break;

case "1": //SD2

DropDownList2.clearItems();

DropDownList2.addItem("Single Occ", "0");

DropDownList2.addItem("Double Occ", "1");

DropDownList2.addItem("Triple Occ", "2");

DropDownList2.addItem("Quad Occ", "3");

break;

default:

// place any error messages here

break;

}



The advantage of the 'switch' statement is the avoidance of the hard to read nested "if" statements, executes the first block of code that meets the logical condition, allows the stacking of the "case" clause to allow the execution of a single block of code for many values, and that the "switch" statement can be configured to evaluate very complex logical relationships.