Tails
05-31-2003, 05:00 PM
Can I nest IF statements? I know I can nest them within ELSE, but I want to be very precise in one statement (or I'll end up using 4 else statements).
Here's what I want. I have 2 checkboxes, and I want this function to fire only when both, or none are checked. I tried this:
D=document.getElementById
if((D('xh1b').checked&&D('xh3b').checked)||(D('xh1b').checked==false&&D('xh3b').checked==false))
{
alert('ok')
}
else
{
alert('only 1 checked (odd)')
}
Notice how I separated one statement with parentheses and then had || with another statement? It threw me no errors, but it didn't work. Instead, I did this:
p_=0
if(D('xh1b').checked){p_+=1}
if(D('xh3b').checked){p_+=1}
if(Math.floor(p_/2)==p_/2){alert('ok')}else{alert('1 checked (odd)')}
Was this the best solution? I hated having to store with a temporary variable because this function would keep initializing all that each fire of the main function.
Here's what I want. I have 2 checkboxes, and I want this function to fire only when both, or none are checked. I tried this:
D=document.getElementById
if((D('xh1b').checked&&D('xh3b').checked)||(D('xh1b').checked==false&&D('xh3b').checked==false))
{
alert('ok')
}
else
{
alert('only 1 checked (odd)')
}
Notice how I separated one statement with parentheses and then had || with another statement? It threw me no errors, but it didn't work. Instead, I did this:
p_=0
if(D('xh1b').checked){p_+=1}
if(D('xh3b').checked){p_+=1}
if(Math.floor(p_/2)==p_/2){alert('ok')}else{alert('1 checked (odd)')}
Was this the best solution? I hated having to store with a temporary variable because this function would keep initializing all that each fire of the main function.