If you are dealing with a list, you only need to look at the brothers and sisters. The list itself contains only li elements and nothing else, so it will be enough:
$(this).siblings().andSelf();
If you are not sure about the name of the node, you can remove it from the first element:
var elem = $("#container :first-child"); elem.siblings( elem.prop("nodeName") ).andSelf();
If you need to follow this logic over and over, it might be wise to provide you with a method by extending jQuery.fn:
jQuery.extend( jQuery.fn, { typeSiblings: function(){ return this.siblings( this.prop("nodeName") ); } });
Allows you to call a new method, like any other filtering method:
$("#container :first-child").typeSiblings().andSelf();
So, in this case, any type of the first child is the types of siblings that we will retrieve. If this is a paragraph, we will get all the paragraphs. If this is an image, we will get all the images. Please note, however, that this is based only on the tag and not on the class (although that would be just as easy to accomplish).
source share