2

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 Answers 3

4

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).

Sign up to request clarification or add additional context in comments.

6 Comments

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?
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... ?
@fredrik: return false is equivalent to preventDefault() and stopPropagation().
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: i corrected one typo (period at the end of the line after hide(()) and it works perfectly now.
|
1

This is a simpler example ..

 <script>
      $(document).ready(function(){
            $('#divtag').click(function(){  //use # for id and . for class
                $('#insidetag').show();     // with parameters slow,fast, or a time in milisecond
            });                             // use toggle to show and hide 
        });
    </script>

<body>
    <div id="divtag">
        <div id="insidetag">
            Click the hide and show
        </div>
    </div>
</body>

1 Comment

Your simple comment //use # for id and . for class saved me hours!! Thanks gifted!
0

jQuery(".user-profile-info").unbind().click(function(){
    if(jQuery( ".user-profile-info" ).hasClass("collapsed")){
        jQuery('#user-profile-submenu').css('display', 'block');
        jQuery( ".user-profile-info" ).removeClass("collapsed");
    }
    else
    {       
        jQuery( ".user-profile-info" ).addClass("collapsed");
        jQuery('#user-profile-submenu').css('display', 'none');
    }
});

1 Comment

<div class="user-profile-info collapsed"></div> <div id="user-profile-submenu">Content of click</div>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.