Garry Jones wrote:
[snip]
How do I nest if statements?
Precisely the way you posted, though in the posted case, you can just
use a single if statement and combine the conditions with the logical
AND operator (&&).
I tried this, but it triggered the alert if just one of the three
conditions was met.
[snip]
if (form.blue.value == "yes") {
Is the control a checkbox? If so, it's value will always be the same
regardless of whether the control is checked. You should check the
checked property.
if (form.green.value == "yes") {
if (form.yellow.value == "yes") {
Another consideration is that if all of these represent values within a
common category (colours, here), give the checkboxes the same name[1]
and use their values to indicate the option chosen:
<input name="colour" type="checkbox" value="blue">
<input name="colour" type="checkbox" value="green">
<input name="colour" type="checkbox" value="yellow">
To check the number of checked controls within that group, loop through
the resulting collection:
function countCheckedControls(controlGroup) {
var count = 0;
for (var i = 0, n = controlGroup.length; i < n; ++i)
if (controlGroup[i].checked) ++count;
return count;
}
var checkedColours = countCheckedControls(form.elements.colour);
[snip]
Mike
[1] For PHP, the name would be "colour[]". This will create an
array within the $_GET superglobal array with the name
"colour". Each element of this will contain the values of the
checked checkboxes. To use that name in a script, use bracket
notation:
form.elements['colour[]']