PDA

View Full Version : Forms and Switches


Dalziel
01-01-2003, 07:09 PM
I've got an if else ladder with about 15 statements in it, (two of which are shown at the bottom as an example) it would be shorter if I could convert it to a switch but I can't get it to work can it be done?


if (document.tester.you.value == "" || document.tester.you.value == "Enter your name...")
// Checking the value has been altered
{
alert("Please enter your name before using the slug tester.");
return false;
}

else if (document.tester.you.value == "Dan Leitch")
{
alert("Hi, Dan Leitch, you want a boot in the chops!");
return false;
}

Vladdy
01-01-2003, 07:27 PM
sinse you are using return from every if statement, you do not need elses:

if(this)
{ // do something
return;
}
if(that)
{ // do something
return;
}
if(somethingelse)
{ // do something
return;
}

switch statement would be feasible if you are testing the same variable for different values.

Dalziel
01-01-2003, 07:44 PM
I know that about if and else, the reason I have used else if is I have just used else at the end. I'm testing the contents of an input box (not a variable) for different values, can a switch do that?

whammy
01-01-2003, 08:21 PM
switch(document.tester.you.value) {
case "":
//do something;
break;
case "Dan Leitch"
//do something else;
break;
default:
//do something else;
}


http://www.javascriptkit.com/javatutors/switch.shtml