jQuery closest Method


jQuery closest Method :It is used to get first ancestor element of the current selection. If you want to get first ancestor element of the selected element use closest method which accepts filter as input parameter.


Syntax of jQuery closest Method

Here is syntax for closest Method example in jQuery –

$(selector).closest(filter);

filter String containing the selector expression for matching the elements.

jQuery Traversing – closest Example

jQuery closest() Method – Example

<script type="text/javascript">// <![CDATA[
         $(document).ready(function(){         
             $( "a" ).closest("ul").css( "background","yellow" );      
          });
// ]]></script>

Try it »

The above code will produce following result-

Note : This is image of the output. To run this demo click on the above “Try it” button.

jQuery closest Method


Advertisements

Add Comment

📖 Read More