$('#cont > fieldset').each(
function(index){
var $self = $(this);
// Here how to get child elements? How to write this selector?
//$('$self > div') ?? this seems does not work.
});
Asked
Active
Viewed 8,517 times
2

user469652
- 48,855
- 59
- 128
- 165
4 Answers
2
$self.find("div"); // return all descendant divs
or:
$self.children("div"); // return immediate child divs
depending on whether you want immediate children or any descendants.
You can even do this to get immediate child divs, but children
is prettier :
$self.find(">div");

karim79
- 339,989
- 67
- 413
- 406
-
This will find descendants, not just children. – Quentin Nov 23 '10 at 11:50
-
Thanks David, was writing that in. – karim79 Nov 23 '10 at 11:51
1
Look at the .children
method in jQuery. This will get direct children of the element, e.g.:
$self.children('div') // returns divs that are direct children
You can also use the similar .find
method if you need to go deeper than one level.
$self.find('div') // returns divs that are direct children, or children of children
Also, you can select using $self
as the context, like:
$('div', $self) //returns all divs within $self

sje397
- 41,293
- 8
- 87
- 103
0
You can use the children() method, to get all immediate children of self.
var children = $self.children();