Jquery: Hide all children, then show a specific element

Christian Bekker picture Christian Bekker · Jan 5, 2012 · Viewed 86.1k times · Source

I want to hide all child elements in a div. And then show a specific one passed on to the function.

function subDisplay(name) {
   $("#navSub").each(function() {
      $(this).hide();
   });
   $(name).show();
}

then i call the function from an onmouse event like: subDisplay(#DivIwantToShow); But nothing displays...

What am i doing wrong?

Answer

Brandon Kindred picture Brandon Kindred · Jan 5, 2012

You need to hide the children and not the containing div.

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

So now if the div you are trying to show is an element in the parent div it will still show while the others stay hidden.