- 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 find all the siblings for the clicked element in jQuery?
To find all the siblings for the clicked element in jQuery, use the parent and siblings method, and select the class to find all siblings.
Example
You can try to run the following code to find all the siblings for the clicked element in jQuery:
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script> $(document).ready(function(){ $(".target").click(function(){ $(this).parent().siblings().removeClass("selectedClass"); $(this).parent().addClass("selectedClass"); }); }); </script> <style> a { cursor:pointer; } .selectedClass{ color:blue; } </style> </head> <body> <h1>Countries</h1> <ul> <li><a class="target">India</a></li> <li><a class="target">US</a></li> <li><a class="target">UK</a></li> </ul> </body> </html>
Advertisements