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!
if (condition1) statement1 else if (condition2) statement2 else if (condition3) statement3 // … To execute multiple statements within a clause, use a block statement ( { /* ...
The if and if-else statements can be nested, meaning they can be used inside another if or else statement.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With