
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the difference between jQuery.children( ) and jQuery.siblings( ) in jQuery?
jQuery children() method
The children( [selector] ) method gets a set of elements containing all of the unique immediate children of each of the matched set of elements.
Here is the description of all the parameters used by this method −
- selector − This is an optional argument to filter out all the childrens. If not supplied then all the childrens are selected.
Example
You can try to run the following code to learn how to work with jQuery children() method:
<html> <head> <title>jQuery children() method</title> <script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("div").children(".selected").addClass("blue"); }); </script> <style> .blue { color:blue; } </style> </head> <body> <div> <span>Hello</span> <p class = "selected">Hello Again</p> <div class = "selected">And Again</div> <p class = "biggest">And One Last Time</p> </div> </body> </html>
jQuery siblings() method
If you want to get the sibling elements, then use the jQuery siblings() method. It returns all the sibling elements of the selected element.
Example
You can try to run the following code to learn how to work with siblings() method in jQuery:
<!DOCTYPE html> <html> <head> <style> .myclass * { display: block; border: 2px solid green; color: green; padding: 3px; margin: 10px; } </style> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("li.new").siblings().css({"color": "blue", "border": "4px solid blue"}); }); </script> </head> <body> <div style="width:600px;" class="myclass"> <ol>ol <li>li</li> <li>li</li> <li class="new">li- This is the sibling.</li> <li>li</li> <li>li</li> </ol> </div> </body> </html>
- Related Questions & Answers
- What is the difference between jQuery and JavaScript?
- What is the difference between jQuery and AngularJS?
- What does jQuery.children() method do in jQuery?
- What is the difference between jQuery(selector) and $(selector)?
- What is the difference between Ajax and jQuery-Ajax methods in jQuery?
- What is the difference between filter() and find() in jQuery?
- What is the difference between text() and html() in jQuery?
- What is the difference between Grep and Filter in jQuery?
- What is the difference between event.preventDefault() and event.stopPropagation() in jQuery?
- What is the difference between append() and appendTo() in jQuery?
- What is the difference between height and innerHeight in jQuery?
- What is the difference between height and outerHeight in jQuery?
- What is the difference between innerHeight and outerHeight in jQuery?
- What is the difference between width and innerWidth in jQuery?
- What is the difference between width and outerWidth in jQuery?
Advertisements