jQuery .has(): Select Elements with Matching Descendants

jQuery’s .has() keeps only the elements in the current selection that contain at least one matching descendant. The result is the containing element. For example, $(".card").has(".badge") keeps the cards containing a badge.

Test descendants with a selector or DOM element

Pass a selector such as ".badge" or a DOM element, an HTML element in the document, to jQuery’s .has() documentation. With a selector, jQuery checks each card’s descendants. A badge can be an immediate child or several levels down.

const $cards = $(".card");
const notice = document.getElementById("notice");

$cards.has(".badge");
$cards.has(notice);

To match the exact badge you already found, pass the DOM element as in .has(notice). Here notice is the badge looked up by its ID. If the card itself has the badge class and has no matching descendant, .has() excludes it.

Run the complete example in a browser

Save the code as a UTF-8 file named example.html and open it in a browser. It loads jQuery 4.1.0 from an external server and needs an internet connection. The $(function () { ... }) callback runs when the document is ready.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Filter cards with jQuery .has()</title>
  <style>
    body { max-width: 42rem; margin: 1rem auto; padding: 0 1rem; font: 1rem/1.5 system-ui; }
    .card { padding: .5rem; margin: .5rem 0; border: 2px solid #777; }
    .marked { background: #fff0b3; border-color: #835b00; }
    .badge { font-weight: bold; }
    button { font: inherit; }
    dl { overflow-wrap: anywhere; }
  </style>
  <script src="https://code.jquery.com/jquery-4.1.0.min.js"></script>
</head>
<body>
  <div class="card" id="a"><p>a <span class="badge" id="notice">Notice badge</span></p></div>
  <div class="card" id="b"><p id="b-content">b Regular content</p></div>
  <div class="card badge" id="c"><p>c The badge class is on this card itself</p></div>
  <button type="button" id="add">Add a badge to b</button>
  <dl aria-live="polite">
    <dt>.has(".badge")</dt><dd id="selector-result">—</dd>
    <dt>.has(element)</dt><dd id="element-result">—</dd>
    <dt>.find(".badge")</dt><dd id="find-result">—</dd>
    <dt>.filter(".badge")</dt><dd id="filter-result">—</dd>
    <dt>Saved result</dt><dd id="saved-result">—</dd>
    <dt>Result after rerunning</dt><dd id="latest-result">—</dd>
  </dl>
  <script>
    $(function () {
      const $cards = $(".card");
      const notice = document.getElementById("notice");
      const $saved = $cards.has(".badge");
      const ids = items => items.get().map(element => element.id).join(", ") || "—";

      $("#selector-result").text(ids($saved));
      $("#element-result").text(ids($cards.has(notice)));
      $("#find-result").text(ids($cards.find(".badge")));
      $("#filter-result").text(ids($cards.filter(".badge")));
      $("#saved-result").text(ids($saved));
      $("#latest-result").text(ids($saved));
      $saved.addClass("marked");

      $("#add").on("click", function () {
        $("<span>", { id: "added-badge", class: "badge", text: " New badge" }).appendTo("#b-content");
        const $latest = $cards.has(".badge");
        $("#saved-result").text(ids($saved));
        $("#latest-result").text(ids($latest));
        $cards.removeClass("marked");
        $latest.addClass("marked");
        $(this).prop("disabled", true);
      });
    });
  </script>
</body>
</html>

Only card a is highlighted at first. Its badge sits inside a paragraph, and card c has the badge class on the card itself. The IDs returned by each method appear below the cards.

Compare what has(), find(), and filter() return

Use jQuery .find() to work with the badges inside the cards. Use jQuery .filter() to check whether the cards themselves have the badge class. The table shows the initial results before you press the button.

Call Element tested or selected Returned ID
.has(".badge") Cards with a descendant matching the badge selector a
.has(notice) Cards containing the notice element a
.find(".badge") Badges inside the cards notice
.filter(".badge") Cards with the badge class on the card itself c

To express the filter as part of a jQuery selector, use the jQuery :has() selector. For example, $(".card:has(.badge)") selects the same cards in this demo. For a collection you already have, $cards.has(".badge") narrows it.

Run the filter again after descendants change

Press the button to add a badge to card b. The saved result still contains a, and another call to $cards.has(".badge") returns a and b. The example uses the fresh result to update the highlights. If you add new cards too, run $(".card") again before filtering.

If the result is empty, check $(".card").length first and inspect whether the badge is inside a card. If you are passing an element, confirm that getElementById() found it.