gpopoteur
gpopoteur

Reputation: 1519

Select only parent nodes that match the selector

I'm trying to get all the parent nodes that contain the .export class, any children node that has a parent with the .export the class should be skipped from the selection.

The nodes could appear anywhere in the tree but want any node that doesn't have a parent with that same class.

Having the following HTML structure:

<div class="export"> <!-- Select this one -->
    <div class="export"> <!-- SKIP this one -->
        <!-- Content -->
    </div>
</div>
<div class="export"> <!-- Select this one -->
    <!-- Some more content -->
</div>

Upvotes: 0

Views: 62

Answers (2)

Zakaria Acharki
Zakaria Acharki

Reputation: 67505

You could get them using the jQuery .not() method like:

console.log($(".export").not(".export .export").length);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="export">
  <!-- Select this one -->
  <div class="export">
    <!-- SKIP this one -->
    <!-- Content -->
  </div>
</div>
<div class="export">
  <!-- Select this one -->
  <!-- Some more content -->
</div>

Upvotes: 1

hsl0
hsl0

Reputation: 15

You can use :not() selector for query to skip that matches the query in the selector.

$('.export:not(.export >)')

Upvotes: 0

Related Questions