Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple IF's and ELSE IF's in javascript

So what's getting pushed to this array is dependant on a few radio boxes. I've got this for standard and wheelchair seats:

if(document.getElementById('standardseat').checked) {
  //Standard seat is checked
seatsArray.push(e.posX, e.posY);
}
else if(document.getElementById('wheelchairseat').checked) {
  //Wheelchair seat is checked
seatsArray.push("Wheelchair " + e.posX, e.posY);
}

And this is the equivalent form code:

<input id="standardseat" type="radio" name="seat" value="standard" /> Standard seat
<input id="wheelchairseat" type="radio" name="seat" value="wheelchair" /> Wheelchair seat

But I want to add in some more radio boxes, which are separate from the standard/wheelchair seat:

<input id="backnave" type="radio" name="area" value="backnave" /> Back Nave
<input id="frontnave" type="radio" name="area" value="frontnave" /> Front nave
<input id="middlenave" type="radio" name="area" value="middlenave" /> Middle nave

And I want the push to also include this. To explain, if the user ticked "Wheelchair seat" and "Middle nave", the push should output ("MN, Wheelchair " + e.posX, e.posY). Is there any way of making this happen without manually including a lot of else if's for each possible outcome (I may even want to add a third set of radio boxes)?

Thanks!

like image 928
IceDragon Avatar asked Jul 13 '10 01:07

IceDragon


People also ask

How do you use multiple if else statements in JavaScript?

if (condition1) statement1 else if (condition2) statement2 else if (condition3) statement3 // … To execute multiple statements within a clause, use a block statement ( { /* ...

Can you use multiple if statements in a function JavaScript?

The if and if-else statements can be nested, meaning they can be used inside another if or else statement.

Should I use multiple if or else if?

Use two if statements if both if statement conditions could be true at the same time. In this example, both conditions can be true. You can pass and do great at the same time. Use an if/else statement if the two conditions are mutually exclusive meaning if one condition is true the other condition must be false.

Can I put an if statement in an if statement JavaScript?

nested-if statement: JavaScript allows us to nest if statements within if statements. i.e, we can place an if statement inside another if statement.


1 Answers

I would build up the string that describes the chair with a comparatively small number of if statements, and then call the push at the end.

So something like:

var desc = "";
if(document.getElementById('wheelchairseat').checked) {
  //Wheelchair seat is checked
  desc = "Wheelchair "+desc;
}

if(document.getElementById("backnave").checked) {
  desc = "BN, "+desc;
} else if(document.getElementById("middlenave").checked) {
  desc = "MN, "+desc;
} else if(document.getElementById("frontnave").checked) {
  desc = "FN, "+desc;
}

seatsArray.push(desc + e.posX, e.posY);

This can easily be extended to account for additional groups of blocks.

like image 134
Jeffrey Blake Avatar answered Nov 14 '22 23:11

Jeffrey Blake