What is the use of sibling method in jQuery?
Use of sibling method in jQuery
265
28-Jul-2021
Aryan Kumar
14-Jun-2023Sure, the sibling() method in jQuery is used to select all sibling elements of the selected element. Sibling elements are elements that share the same parent.
The sibling() method takes no arguments and returns a jQuery object containing all of the sibling elements of the selected element.
Here is an example of how to use the sibling() method:
Code snippet
The siblings object can be used to perform a variety of tasks, such as:
The sibling() method is a powerful tool that can be used to easily select all of the sibling elements of an element.
Here are some additional examples of how the sibling() method can be used:
// Change the color of all of the siblings of the element with the id 'my-element' to red $('#my-element').siblings().css('color', 'red');
// Add an event handler to all of the siblings of the element with the id 'my-element' to prevent them from being deleted $('#my-element').siblings().on('click', function() { return false; });
// Get the value of the href attribute on all of the siblings of the element with the id 'my-element' var hrefs = $('#my-element').siblings().attr('href');