How to work with jQuery.closest() method in jQuery?

The closest() method begins with the current element and returns only the first ancestors matching the passed expression. This returned jQuery object has zero or one element.


You can try to run the following code to learn how to work with jQuery closest method:

Live Demo

<!DOCTYPE html>
.myclass * {
    display: block;
    border: 2px solid blue;
    color: red;
    padding: 5px;
<script src=""></script>
    $("span").closest("ol").css({"color": "gray", "border": "3px solid yellow"});

<body class="myclass">body
  <div style="width:500px;">div
    <ol>ol - This is the third ancestor
    <ol>ol - This is the second ancestor
    <ol>ol - This is the first ancestor
      <li>li - This is the direct parent element