- 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 use jQuery.closest() method with class selector?
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. Using the class selector is easy and let us see how to use it below.
Example
You can try to run the following code to learn how to use jQuery.closest() method with class selector:
<!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(".one").css({"color": "maroon", "border": "5px solid maroon"}); }); </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 <li>li - This is the direct parent element <span>demo</span> <span class="one">demo - using class selector with closest() method</span> </li> </ol> </ol> </div> </body>
- Related Articles
- How to work with jQuery.closest() method in jQuery?
- How to combine a class selector and an attribute selector with jQuery?
- How do we use .not() with jQuery selector?
- How to use subSet() method of Java NavigableSet Class
- How to use headSet() method of Java NavigableSet Class
- How to use the click() method in Action Chain class in Selenium with python?
- jQuery .class Selector
- How to use universal (*) selector in jQuery?
- How to use isAlive() method of Thread class in Java?
- How to use ReadKey() method of Console class in C#?
- How to use WriteLine() method of Console class in C#?
- How do we use jQuery Attribute selector with a period?
- How to use interface with class in TypeScript?
- How to use *= operator in jQuery attribute selector?
- How to use the Clear method of array class in C#?

Advertisements