1

I have a container for all my elements and I hide all of its children like so :

$("#svgContainer").children().hide();

I have two elements that are the 'SVGContainer''s grandchildren :

#canvasParent & #canvasChild.

I have tried doing this :

$("#svgContainer").children().hide();
        $("#canvasParent").show();
        $("#canvasChild").show();

This doesn't seem to work, probably because the display:none; gets given to the parent not the child.

How do I go about hiding every one of the SVGContainers children except for the elements with id's : #canvasParent & #canvasChild.

Here's a fiddle of the layout I have : http://jsfiddle.net/o9zowx3b/1/

Notice it hides all elements still, I think this is due to them being grandchildren not children of the svgContainer

thatOneGuy
  • 9,977
  • 7
  • 48
  • 90

4 Answers4

2

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:

$("#svgContainer").children().hide();​​​​

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

Ivin Raj
  • 3,448
  • 2
  • 28
  • 65
2

This should work as requested.

$('#svgContainer').children().not('#canvasParent').not('#canvasChild').hide();

Here is a fiddle, specially for you!

Good luck.

LaVomit
  • 492
  • 5
  • 15
  • you are amazing haha this works great. However, ive forgot to mention the way mine is set out. The canvasParent&canvasChild are grandchildren not children of the svgContainer. Are you able to amend this updated fiddle ? http://jsfiddle.net/o9zowx3b/1/ You can see it hides everything :( – thatOneGuy Sep 08 '15 at 10:39
1

This will select all children except the 2 children.

$("#svgContainer").children().not("#canvasParent,#canvasChild").hide();
Sandeep
  • 819
  • 10
  • 16
1

You can also do it with single selector:

$("#svgContainer > :not(#canvasParent,#canvasChild)").hide();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="svgContainer">
    <div id="canvasParent">canvasParent</div>
    <div>should be hidden</div>
    <div id="canvasChild">canvasChild</div>
</div>
dfsq
  • 191,768
  • 25
  • 236
  • 258
  • I have mentioned this in another comment but not in the question.Both canvasParent & canvasChild are granchildren of the svgContainer. So none of these methods are working. I've updated a fiddle @LaVomit created. http://jsfiddle.net/o9zowx3b/1/ in this it has the same layout as my problem. Appreciate it if you can take a look :) – thatOneGuy Sep 08 '15 at 10:41
  • 1
    Then selector will be like this: `$("#svgContainer > #svg > :not(#canvasParent,#canvasChild,#showMe)").hide();`. Demo: http://jsfiddle.net/o9zowx3b/2/ – dfsq Sep 08 '15 at 10:43