views:

29

answers:

4

I'm trying to do something like the following in jQuery

$('.container').siblings('.outerClass > .innerClass')

where I'm looking for:

<div class="container"></div>

<div class="outerClass">
    <div class="innerClass">
        find me!
    </div>
</div>

I can't get the syntax right.

A: 

Is there a problem ommitting the container selector, and just using

$('.outerClass > .innerClass')

or perhaps (if you don't want to require the inner div to be a direct child)

$('.outerClass .innerClass')
Tomas Lycken
yeah I need the container siblings part unfortunately. I tried both of those and I don't think it worked (unless I've made a mistake calculating the html).
fearofawhackplanet
A: 

You can also do this to directly select the required elements

$(".outerClass").children(".innerClass").click(function() {
//Do your stuff
});
Starx
A: 
$('.container').siblings('.outerClass:has(> .innerClass)')

To explain why; .outerClass > .innerClass is a selector that selects an element with class innerClass, not the outerClass. To select an element that has something specific in it, you use the :has selector, which takes a selector as an argument.

reko_t
+2  A: 

And another one (but this should work) (assuming you want to get the element with class innerClass):

$('.container').siblings('.outerClass').children('.innerClass')
Felix Kling