zig.com Result Size: 625 x 565
x
 
<!DOCTYPE html>
<html>
<head>
<style>
.ancestors * { 
  display: block;
  border: 2px solid lightgrey;
  color: lightgrey;
  padding: 5px;
  margin: 15px;
}
</style>
<script src="../../ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script>
  $(document).ready(function () {
    $("span").parentsUntil("body", "p, li, div").css({ "color": "red", "border": "2px solid red" });
  });
</script>
</head>
<body class="ancestors"> body (great-great-great-grandparent)
  <div style="width:500px;">div (great-great-grandparent)
    <ul>ul (great-grandparent)  
      <li>li (grandparent)
        <p>p (direct parent)
          <span>span</span>
        </p>
      </li>
    </ul>   
  </div>
</body>
<!-- In this example, we narrow down the search for ancestors by selecting p, li and div between the span and the body element. -->
<!-- /jquery/tryit.asp?filename=tryjquery_traversing_parentsuntil_multi  Fri, 07 Feb 2025 00:12:30 GMT -->
</html>