parentsUntil() Method - Javascript jQuery Method and Property

Javascript examples for jQuery Method and Property:parentsUntil

Description

The parentsUntil() method returns all ancestor elements between the selector and stop.

Syntax

$(selector).parentsUntil(stop,filter);
Parameter Require Description
stop Optional. selector expression, element or jQuery object indicating where to stop the search
filterOptional. selector expression to narrow down the search for ancestors between selector and stop

To return multiple ancestors, separate each expression with a comma.

The following code shows how to Return all ancestor elements between <span> and <div>:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<style>
.ancestors * {//  w  ww .j  a  v  a2  s.c om
    display: block;
    border: 2px solid lightgrey;
    color: lightgrey;
    padding: 5px;
    margin: 15px;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("span").parentsUntil("div", "ul").css({"color": "red", "border": "2px solid red"});
});
</script>
</head>

<body class="ancestors"> body
  <div style="width:500px;">div
    <ul>ul
      <li>li (direct parent)
        <span>span</span>
      </li>
    </ul>
  </div>
</body>

</html>

Related Tutorials