Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery multiple parent() calls

I have this jQuery:

$(this).parent().parent().find(".license_tooltip").stop(true, true).fadeIn(200);

The $(this) object is nested within two divs like this:

<div>
    <div>
        <a href="">$(this) object</a>
    </div>

    <div>
        <a href="">object to fade in</a>
    </div>
</div>

Can someone point me in the right direction to making my jQuery more streamlined? The structure presented above is replicated multiple times, so using classes and IDs is impossible.

like image 863
Bojangles Avatar asked Dec 09 '10 20:12

Bojangles


3 Answers

You can use a class (or any other selectable attribute) and .closest() to claim to the parent you want, like this:

<div class="container">
    <div>
        <a href="">$(this) object</a>
    </div>

    <div>
        <a href="">object to fade in</a>
    </div>
</div>

And for the script:

$(this).closest(".container").find(".license_tooltip").stop(true, true).fadeIn(200);
like image 105
Nick Craver Avatar answered Oct 15 '22 14:10

Nick Craver


You could use the .parents( [ selector ] ) here is a link

It will traverse more than one parent up.

like image 31
Josiah Ruddell Avatar answered Oct 15 '22 15:10

Josiah Ruddell


use parents()

$(this)
    .parents('selector for the parent you need to look in')
    .find(".license_tooltip")
    .stop(true, true)
    .fadeIn(200);
like image 31
Mark Baijens Avatar answered Oct 15 '22 14:10

Mark Baijens