How to subfilter a jquery object that is a subset of $ (this)

I've read the documentation over and over again and I can't figure out why this doesn't work:

From within a function calling the following:

alert($(this).parent().parent().html());

      

returns something similar to this:

<div class="something1">
    <div class="whereThisStarted">stuff</div>
    </div>
<div class="something2">stuff</div>
<div class="somethingSpecial">stuff</div>
<div class="something4">stuff</div>

      

I want to get "somethingSpecial". It seems to me that either of the following should work, but they both return null.

alert($(this).parent().parent().children(".somethingSpecial").html());
alert($(this).parent().parent().filter("div.somethingSpecial").html());

      

What's wrong with that?

thanks

0


a source to share


3 answers


if you really have to do it the way you want and not as TStamper showed, try this:



alert($(this).parent().parent().find("div.somethingSpecial").html());

      

+6


a source


You might want to try the closest function - it sounds the same as what you are trying to do:

jquery doc - closest ()

This code should work:



alert($(this).closest(".somethingSpecial").html());

      

Or in case your $ (this) is inside the 'startHere' div:

alert($(this).parent().closest(".somethingSpecial").html());

      

+2


a source


I'm not sure why children ("div.somethingSpecial") don't work, but I just figured out that this works as well:

alert($(this).parent().parent().children().filter("div.charactersLeft").html());

      

0


a source







All Articles