<input type="radio" id="rad1" name="group" value="one">First Option</input><br>
<input type="radio" id="rad2" name="group" value="two">Second Option</input><br>
<input type="radio" id="rad3" name="group" value="three">Third Option</input><br>
<br>
<input type="radio" id="rad7" name="group2" value="one">First Option</input><br>
<input type="radio" id="rad8" name="group2" value="two">Second Option</input><br>
<input type="radio" id="rad9" name="group2" value="three">Third Option</input><br>
The following statements are true
$('[value="one"]').length == 2
$('[name="group2"]').length == 3
So how come..
$('[value="one"]',$('[name="group2"]')).length == 0
?
I would have thought that this should effectively look within all elements with name==group2 for elements with value=="one" ie 1. Why is this not the case?
This selector, using the second argument for scope/context:
Attempts to find elements with
[value="one"]that descend from elements with[name="group2"]. That means you end up looking down at least two levels of different elements in the DOM.In your case, your radio buttons have both attributes, so you want to attach both attribute selectors instead (I add
inputjust to save jQuery some work):