Steven Zack
Steven Zack

Reputation: 5104

jQuery how to hide all the first level children div items in a div at one time?

I have a complex div part on my page which combined with divs as following:

<div class="parent">
  <div class="child1">
    <div class="grandchild1" />
    <div class="grandchild2" />
  </div>
  <div class="child2">
    <div class="grandchild3"></div>
    <div class="grandchild4"></div>
  </div>
</div>

I wonder how to hide div child1 and child2 at one time, I know $(' .parent div') will hide all the div inside of .parent div, I don't want to hide grandchildren level, because when I set child1 visible, the grandchild1 and grandchild2 are still invisible. Thanks for help.

Upvotes: 0

Views: 769

Answers (3)

ShankarSangoli
ShankarSangoli

Reputation: 69905

Use the child selector.

$('.parent > div').hide();

Upvotes: 0

Andrew
Andrew

Reputation: 13853

You can use the child selector,

$('.parent > div').hide();

or get the children

$('.parent').children().hide();

Upvotes: 1

Blender
Blender

Reputation: 298166

Use the direct descendent CSS selector:

$('.parent > div').hide();

Upvotes: 4

Related Questions