Jatin Jatin - 7 months ago 9
HTML Question

If checkbox is checked, then select one radio

I have a checkbox "CR". If it is checked it opens up a div which contains 2 radio buttons("IA" and "FA"). Now, if the user checks "CR" but does not select at least one radio, it should throw an alert that "At least one must be selected."
In my case, The alert pops up even if one of the radio button is checked. I am guessing I am missing on something very minute but couldn't figure out.
Below is the code:

<input type="checkbox" name="my_cr" id="CR"/>CR
<input type="radio" id="IA" name="Initial" />IA
<input type="radio" id="FA" name="Final" />FA
<input type="submit" />

<script>
else if(this.CR.checked)
{
if((!this.IA.checked) || (!this.FA.checked))
{
alert("Choose the approver type");
return false;
}
}
</script>


PS: "Else if" because, I am doing a couple of other validations too.
Thanks for the help.

Answer

Your or (||) should be an and (&&).

Right now, if either is unchecked, the alert pops up.

Comments