Stefanvds
Stefanvds

Reputation: 5916

select first object where parent does not have class

HTML looks like this

<tr class="rowinactive">
<td>
<div class="controllingbuttons">
<a title="Aanpassen" href="/2013-2014/Persoon/Edit/1811?instID=410">
<img width="16" alt="Edit" src="/img/pencil.png">
</a>
...
</td>
</tr>

OR

<tr> (rowinactive class missing)
<td>
<div class="controllingbuttons">
<a title="Aanpassen" href="/2013-2014/Persoon/Edit/1811?instID=410">
<img width="16" alt="Edit" src="/img/pencil.png">
</a>
...
</td>
</tr>

now, I want to count the amount of images in the first div 'controllingbuttons', but its parent TR can not have the 'rowinactive' class.

my code previously (when I didnt care about rowinactive yet)

$(".controllingbuttons").first().parent("td").find("img").length

I've been trying this, but it doesnt work:

$(".controllingbuttons").not($(".controllingbuttons").parents(".rowinactive").find(".controllingbuttons")).first().parent("td").find("img").length

any ideas?

Upvotes: 0

Views: 50

Answers (4)

user2412511
user2412511

Reputation: 29

$(".controllingbuttons").parent('tr:not(.rowinactive)').first()

or

$(".controllingbuttons").closest('tr:not(.rowinactive) td:First').length

Upvotes: 0

Jai
Jai

Reputation: 74738

You can tryout this:

var imageLen = $(".controllingbuttons").closest('tr:not(.rowinactive)').each(function(){
    return $(this).find('img').length;
});

Upvotes: 1

xdazz
xdazz

Reputation: 160853

$('tr').not('.rowinactive').find('.controllingbuttons img').length

Upvotes: 2

Felix
Felix

Reputation: 38102

Try to use closest() along with :not():

$(".controllingbuttons").closest('tr:not(.rowinactive)').length

Upvotes: 1

Related Questions