Reputation: 7776
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
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
Reputation: 7465
$(document).ready(function () {
$('div#parent-1 > div:eq(2)').css("display", "none");
});
The >
does it all.
Upvotes: 5
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