42

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?

Manse
  • 37,765
  • 10
  • 83
  • 108
Christian Bekker
  • 1,857
  • 4
  • 27
  • 43
  • 3
    What is passed in `name`? Is it a jQuery object, or a selector? Also, looking at your code it appears you have more than one `#navSub`, which is illegal HTML - you can only have one unique ID in the page - use classes instead. – Bojangles Jan 05 '12 at 14:49
  • are you literally calling `subDisplay(#DivIwantToShow);`? – J. Holmes Jan 05 '12 at 14:49
  • 3
    You need `subDisplay('#DivIwantToShow');` -- it's a string that you pass there – js-coder Jan 05 '12 at 14:49
  • 2
    FYI, you can optimize your code by calling `$('#navSub').hide()`, rather than iterating over them using `each()` – Matt Jan 05 '12 at 14:50
  • Are you actually using `subDisplay(#DivIWantToShow)` or are you using `subDisplay("#DivIWantToShow")`? Also, what happens if, in subDisplay, you `console.log(name)`? – Wing Jan 05 '12 at 14:54

7 Answers7

90

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.

Brandon Kindred
  • 1,468
  • 14
  • 21
5

If you're targeting the children of #navSub, you need target them and hide them, rather than the element navSub; which you can do using the children() method;

function subDisplay(name) {
    $('#navSub').children().hide();
    $(name).show();
};

Otherwise, it appears you have multiple elements with the same ID in your DOM, which is not allowed.

You then need to pass a string (which is a valid jQuery selector) to subDisplay();

subDisplay('#DivIwantToShow');
Matt
  • 74,352
  • 26
  • 153
  • 180
1
function subDisplay(name) {
   $("#navSub").hide();
   $('#'+name).show();
}
kaz
  • 1,943
  • 1
  • 13
  • 19
1

To summarize the great comments from @dotweb and @Matt;

function subDisplay(name) {
   $('#navSub').hide();
   $(name).show();
}

subDisplay('#DivIwantToShow');
Stefan
  • 5,644
  • 4
  • 24
  • 31
1

if the name of the element is passed in name use this:

    if($(this).attr('name') != name){
    //Hide it
    } else {
   //show it
}
Teun Pronk
  • 1,367
  • 12
  • 24
0

If you want to hide child div using class.

<div class="parent_div">
    <div class="child_div">1st div</div>
    <div class="child_div">2nd div</div>
</div>

$(document).ready(function(){
    $(".parent_div").on('click',function(){
        $j(this).find("div").toggle();
    })
})
Chaitanya Bhojne
  • 156
  • 4
  • 10
0

Try having it outside of the loop, like so:

function subDisplay(name) {

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

     $(name).show();
}

http://jsfiddle.net/peduarte/m2pj8/

peduarte
  • 1,667
  • 3
  • 16
  • 24