Bug Tracker

Opened 12 years ago

Closed 12 years ago

Last modified 10 years ago

#9603 closed enhancement (invalid)

Give siblings() the option to provide siblings which are not part of itself.

Reported by: niklas Owned by: niklas
Priority: low Milestone: 1.next
Component: traversing Version: 1.6.1
Keywords: Cc:
Blocked by: Blocking:


Currently, using siblings() on a selection which contains multiple elements under the same parent, will return all elements under the parent.

With the following markup:

<li class="select">2</li>
<li class="select">3</li>


$('ul li.select').siblings().length;

Would return all 4 elements.

Instead, I propose that the siblings should have a second optional boolean variable which would define whether to select only the siblings which aren't part of the selection the method is applied on. That can easily be done with a custom plugin like this:

(function( $ ){
  $.fn.uniqueSiblings = function(selector) {
	 return $(this).siblings(selector).not(this);
})( jQuery );

But considering how little code it is, why doesn't jQuery have such functionality by default? Personally, I think it is the more logical way for siblings to work for selections with multiple elements anyway, but with providing a boolean variable to choose which way (old way or, this new proposed way) it will continue to work for users like before.

Working test case (using same example as on official siblings() documentation): http://jsfiddle.net/niklasvh/Ax63q/

Change History (4)

comment:1 Changed 12 years ago by dmethvin

Component: unfiledtraversing
Owner: set to niklas
Priority: undecidedlow
Status: newpending

Why not $("ul>li:not(.selected)") instead?

I'll leave this open for a reply, but I would not want to have .siblings add a special-case boolean argument for a case like this.

comment:2 in reply to:  1 Changed 12 years ago by niklas

Status: pendingnew

If you want to do something to the selection, and something that isn't part of the selection, you'd have to do it in 2 different statements, where as with siblings you could first apply it to the selection, and then apply to the rest.

What about element collections which cannot be (easily) described with selectors, if they were composed using add() for example?

Replying to dmethvin:

Why not $("ul>li:not(.selected)") instead?

I'll leave this open for a reply, but I would not want to have .siblings add a special-case boolean argument for a case like this.

comment:3 Changed 12 years ago by dmethvin

Resolution: invalid
Status: newclosed

Sorry but I'm not understanding the need here. Perhaps you can explore and discuss it in the forum, then formulate a specific request if there is interest.

comment:4 Changed 10 years ago by st42

I have the same problem. Why closed? sibling for me is : all but not selected. My sample :

	editors = $('#jform_editor').find(render.editors);

editors is the list of valid editor for eg: ".text,.textarea". and i hide the other. IN my case sibbling is all unvalid editor. But this simply remove all, because sibbling work only in one element. Perhaps a new jQuery().sibblings() ?

Note: See TracTickets for help on using tickets.