views:

18

answers:

1

I have a page with some radio buttons that I want to be checked on page load. This is the jQuery that I'm using:

$('.optionHolder').find('input').first().attr('checked', 'true');

The one's I want checked are the first input elements of each .optionHolder on the page. At the moment there are two, but the code above is only checking one. Take a look at the image:

http://imgur.com/NQkei.jpg

As you can see, only the first set is being checked.

Any help would be appreciated, thanks.

+1  A: 

Try

$('.optionHolder input:first-child').attr('checked', true);

first() will select the first element in a collection, whereas :first-child will select each element only if it is the first child of its parent.

:first works by selecting the first input element that is a child of .optionHolder:

$('.optionHolder input:first')
Andy E
+1 - But why the `.find()`? :)
Nick Craver
had to use: `.find('input:first')` but your solution works, thanks! :)
Will
@Nick, I just removed the *first()* and expanded the OP's selector for *find()*. I'll update it :-) I was looking for a link but the jQuery site is down.
Andy E
@Will: it depended on your HTML structure really - I've updated my answer with your solution too.
Andy E