tags:

views:

7020

answers:

2

What would be the best way to loop through all radio buttons on my page and alert the user if one of them is unchecked for a particular question?

Note: there are multiple questions on one page.

<li>
  <fieldset>
  <h3>What music do you listen to?</h3>  

        <ul class="answerList">
            <li>
                <input type="radio" id="choice_22" name="answer8" value="1" class=""/>
                <label for="choice_22" class="">Heavy Metal  </label>
            </li>
            <li>
                <input type="radio" id="choice_23" name="answer8" value="2" class=""/>
                <label for="choice_23" class="">Pop music</label>
            </li>
            <li>
                <input type="radio" id="choice_24" name="answer8" value="3" class=""/>
                <label for="choice_24" class="">Oh, a mix of stuff </label>
            </li>
         </ul>
       </fieldset>   
      </li>
+7  A: 
if (undefined === $("input[name='answer8']:checked").val()) {
    // do something
}
patridge
Btw in jQuery 1.3 [@attr] style selectors were removed (in 1.2 they were deprecated)
jitter
I updated the syntax to reflect that latest API (simply remove the "@" character and you are good to go). Thanks for pointing that out.
patridge