views:

472

answers:

1

I'm trying to use jquery to show and hide a div onclick, and, although I'm not getting any errors, it's not showing or hiding anything either.

*EDIT - UPDATED * $(document).ready(function() {

        $("#nav-inner a").click(function() {
            var type = $(this).attr("class");
                $("div.v1 > div").not("." + type).stop().hide().end().filter("." + type).stop().show();
                return false;
    });

});

Here's the jquery:

$(document).ready(function() {
        if($("#nav-inner")) {
                $("#nav-inner ul li a").click(function(evt) {
                        var type = $(this).attr("class");
                        var rowcount = 0;
                        $('div.v1 .vc').each(function(idx,el) {
                                if(type == 'typea') {
                                    if($(el).hasClass('typea')) {
                                                $(el).show();
                                        } else {
                                                $(el).hide();
                                        }
                                } 
                        });
                    });
    }
});

And here's the markup:

<div id="page">
    <div id="header">
        <div id="nav">
            <div id="nav-inner">
                <ul class="nav-inner-li">
                    <li>
                        <a class="typea" href="#"><img src="/images/typea.png"></a>
                        <a class="typea" href="#">Type A</a>
                    </li>
                </ul>
            </div>
        </div>
    </div>

    <div id="content">
        <div id="content-content">
            <div id="content-left">
                <div class="v1">
                    <div class="vc">
                        <div class="typea">
                            <div class="title"> Title </div>
                            <div class="body"> Body </div>
                        </div>

                        <div class="typeb">
                            <div class="title"> Title 2 </div>
                            <div class="body"> Body 2 </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
+3  A: 

This can be made much simpler.

$(function() {
  $("#nav-inner a").click(function() {
    var type = $(this).attr("class");
    $("div.vc > div").not("." + type).stop().hide()
      .end().filter("." + type).stop().show();
    return false;
  });
});

Your first error was #nav-inner ul where #nav-inner is actually the ul element. The above grabs the class from the clicked link and then selects the child of div.v1 with that class and toggles it (shows it if hidden, hides it if not).

cletus
I always use e.preventDefault() instead of return false. Correct me if I'm wrong, but doesn't return false only cancel current element's event? and preventDefault() stops the event chain?
fredrik
Thanks... It works, sort of... Except that I need to hide everything except the divs that have that class. So, clicking on <a class="typea"> shows the typea divs and hides all the others... This looks like it's doing the opposite... ?
phpN00b
@fredrik: `return false` is equivalent to `preventDefault()` **and** `stopPropagation()`.
cletus
@phpN00b: try the updated version.
cletus
I tried the revised version, and now it hides everything and doesn't show anything? I'll put the code that I just tried in the edited version of my original post...
phpN00b
@phpN00b: i corrected one typo (period at the end of the line after `hide(()`) and it works perfectly now.
cletus
I saw that, and corrected it (you can see it in the edit), but it's still hiding everything?
phpN00b
Bah, nvm... you're right... it's perfect. thanks!
phpN00b