Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery show/hide divs

Having a html structure like this:

<div id="newest">
    <nav>
        <ul id="newNav">
            <li><a href="#">Dani's Photos</a></li>
            <li><a href="#">Alex's Photos</a></li>
        </ul>
    </nav>
    <ul class="dani">
        <li>........</li>
        <li>........</li>
        <li>........</li>
        <li>........</li>
        <li>........</li>
        ...
    </ul>
    <ul class="alex">
        <li>........</li>
        <li>........</li>
        <li>........</li>
        <li>........</li>
        <li>........</li>
        ...
    </ul>
</div>

Dani's Photos corresponds to ul class="dani" and Alex's photos to ul class="alex". What I need is to show only 1 ul at the time, and show/hide the other when click on the links inside , and showing by default the first ul.

Any ideas?

like image 373
Alex Avatar asked Mar 01 '26 18:03

Alex


2 Answers

Amend your HTML like this:

<div id="newest">
    <nav>
        <ul id="newNav">
            <li><a href="#dani">Dani's Photos</a></li>
            <li><a href="#alex">Alex's Photos</a></li>
        </ul>
    </nav>
    <ul id="dani" class="photos">
        <li>....Dani....</li>
        <li>........</li>
        <li>........</li>
        <li>........</li>
        <li>........</li>
    </ul>
    <ul id="alex" class="photos">
        <li>....Alex....</li>
        <li>........</li>
        <li>........</li>
        <li>........</li>
        <li>........</li>
    </ul>
</div>

Then in jQuery:

$("#newNav A").on("click", function() {
    $(".photos").hide();
    var target = $(this).attr("href");
    $(target).show();
});

Example Fiddle

like image 125
Rory McCrossan Avatar answered Mar 03 '26 09:03

Rory McCrossan


$(function(){
     $(".alex").hide();
});
$("#dan").click(function()
{
   $(".alex").hide();
   $(".dani").show();

});
$("#alex").click(function()
{
    $(".dani").hide();
    $(".alex").show();
});

Here is the sample : http://jsfiddle.net/yFDUB/4/

EDIT : As per your comment, you need a generic solution which works for n number of uls I wrapped the uls in a div with id "divItems" and i am assuming that the class name of the divs and the id of the links will be same.

$(function(){

    $("#divItems ul").hide();
    $("#divItems ul:first").show();
});

$("#newNav a").click(function()
{
  $("#divItems ul").hide();
  var className=$(this).attr("id"); 
  var objToShow=    $("."+className);  
  objToShow.fadeIn();  

});

Here is the sample : http://jsfiddle.net/yFDUB/10/

like image 45
Shyju Avatar answered Mar 03 '26 10:03

Shyju



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!