- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
Example
You can try to run the following code to learn how to work with jQuery closest method:
<!DOCTYPE html> <html> <head> <style> .myclass * { display: block; border: 2px solid blue; color: red; padding: 5px; margin:10px; } </style> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("span").closest("ol").css({"color": "gray", "border": "3px solid yellow"}); }); </script> </head> <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 <span>demo</span> </li> </ol> </ol> </ol> </div> </body>
- Related Articles
- How to use jQuery.closest() method with class selector?
- How does jQuery.clone() method work in jQuery?
- How does jQuery.filter() method work in jQuery?
- How does jQuery.find() method work in jQuery?
- How does jQuery.eq() method work in jQuery?
- How does jQuery.map() method work in jQuery?
- How does jQuery.slice() method work in jQuery?
- How does jQuery.nextAll() method work in jQuery?
- How does jQuery replaceWith() method work?
- How does $('iframe').ready() method work in jQuery?
- How to work with removeEventListener() method in JavaScript?
- How to work with addEventListener() method in JavaScript HTML DOM?
- How does jQuery.add( ) work in jQuery?
- How does CSS Selectors work in jQuery?
- How to use jQuery.wrapInner() method in jQuery?

Advertisements