How to hide all children div except a specific one with jQuery?

Viewed 37369
<div id="target">
    <div id="exclude"></div>
    <div></div>
    ...
</div>

$('#target').children().hide(); will hide all.

5 Answers

What you're wanting to do is hide all of the siblings of a particular element. That's relatively simple with jQuery using the .siblings method:

​$("#exclude").siblings().hide();​​​​

This will hide all elements on the same level, in the same parent element.

I believe that $('#target > div').not('#exclude').hide() should do what you want.

Or alternately if you want sub-children that are divs too, $('#target div').not('#exclude').hide()

Related