July 8, 2010

Find class inside class with jQuery selectors

Question by fearofawhackplanet

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.

Answer by Felix Kling

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

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

Answer by Starx

You can also do this to directly select the required elements

$(".outerClass").children(".innerClass").click(function() {
//Do your stuff
});

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!