Roopendra
Roopendra

Reputation: 7776

Hide third child div in jquery

I am trying to hide third child div in jQuery , but its hide first child div subchild.

I am trying with below code :-

Html :-

<div id="parent-1">
    <div id="child-1" class="child">CHILD 1
       <div id="sub-child-1" class="subchild">SUB CHILD 1</div>
       <div id="sub-child-2" class="subchild">SUB CHILD 2</div>
       <div id="sub-child-3" class="subchild">SUB CHILD 3</div>
    </div>
    <div id="child-2" class="child">CHILD 2</div>
    <div id="child-3" class="child">CHILD 3</div>
</div>

Jquery :-

$(document).ready(function () {
   $('div#parent-1 div:eq(2)').css("display", "none");
});

I need to hide child-3 here. but it is hiding sub-child-3.

here is jsfiddle

Any suggestion??

Thanks in advance.

Upvotes: 4

Views: 7376

Answers (5)

Sumit Kumar
Sumit Kumar

Reputation: 1902

this should work

$('#child-3',$('#parent-1')).hide();

Upvotes: 0

Jai
Jai

Reputation: 74738

very simply do it with css only:

#child-3{
   display:none;
}

Upvotes: 0

Mr_Green
Mr_Green

Reputation: 41832

Try this: (Updated)

$('#parent-1').children('div').eq(2).hide();

or

$('#parent-1 > div').eq(2).hide();

.hide() will work same as display : none;

Later you can use .show() to display the element again. (if you want to).

Upvotes: 1

JNDPNT
JNDPNT

Reputation: 7465

$(document).ready(function () {
   $('div#parent-1 > div:eq(2)').css("display", "none");
});

The > does it all.

Upvotes: 5

GautamD31
GautamD31

Reputation: 28763

Try like this

$(document).ready(function () {
   $('#parent-1 .child').eq(2).css("display", "none");
});

or you can use like

$('#parent-1 > div:eq(2)').css("display", "none");

that represents the relative childs of the 'parent-1'

Upvotes: 2

Related Questions