views:

210

answers:

4

Hi,

I need to set up jquery to cycle through tables, making sure it shows/hides the right one.

The basic HTML structure is:

<p><a href="#" class="month-next">Next Month</a></p>

<table class="month-show"><tr><td>blurb</td></tr></table>
<table class="month-hide"><tr><td>blurb</td></tr></table>
<table class="month-hide"><tr><td>blurb</td></tr></table>
<table class="month-hide"><tr><td>blurb</td></tr></table>

etc

So in jQuery so far I have:

$(".month-next").click(function () {
 $("table.month-show").hide();
 HERE I NEED SOME CODE TO ONLY SHOW THE NEXT TABLE IN THE HTML .show();
 return false;
});

If anyone could help it would be much appreciated.

A.

+4  A: 
$(".month-next").click(function () {
 $("table.month-show").hide().next().show();
 return false;
});

you might also want to change the classes, so that the next call will function as well (edit: inserted changes suggested by Mark Schultheiss):

$(".month-next").click(function () {
    $("table.month-show")
        .hide()
        .removeClass("month-show")
        .addClass("month-hide")
        .next()
        .show()
        .removeClass("month-hide")
        .addClass("month-show");
    if ($("table").hasClass('month-show').length < 1) {
        $('table').eq(0).addClass('month-show');
    }
 return false;
});
harpax
Ok, far better than my answer. Far more fluent. However doesn't take care of cycling back to the first item when the last is visible :P
Jamiec
probably needs a check at the end prior to the return: if($("table").hasClass('month-show').length < 1) $('table').eq(0).addClass('month-show');
Mark Schultheiss
@Jamiec .. and therefore no better than your answer :)
harpax
+1  A: 

Here you go. This should work (and even takes care of cycling). You may have to make some slight adjustments due to other markup on tyour page.

<html>
<head>
<script language="javascript" src="jquery-1.3.2.js"></script>

<script language="javascript">
$(document).ready(function(){

    $(".month-next").click(function () {
     var $monthShow = $("table.month-show");
     var $monthNext = $monthShow.next('table.month-hide');
    if($monthNext.length == 0){
        $monthNext = $('table.month-hide:first');
        }
     $monthShow.removeClass('month-show').addClass('month-hide');
     $monthNext.removeClass('month-hide').addClass('month-show');
     return false;
    });

});
</script>
<style type="text/css">
.month-show{ display:block;}
.month-hide{ display:none;}

</style>
</head>
<body>

<p><a href="#" class="month-next">Next Month</a></p>

<table class="month-show"><tr><td>blurb1</td></tr></table>
<table class="month-hide"><tr><td>blurb2</td></tr></table>
<table class="month-hide"><tr><td>blurb3</td></tr></table>
<table class="month-hide"><tr><td>blurb4</td></tr></table>

</body>
</html>
Jamiec
+1  A: 

You can try this:

<script type="text/javascript">
    $(function () {
        $(".month-hide").hide();
        $(".month-next").click(function () {
            $(".month-show+table").addClass("month-show").removeClass("month-hide").show();
            $(".month-show").filter(":first").removeClass("month-show").hide();
        });
    });
</script>
VinTem
+2  A: 

This is a working example of how I would it: http://jsbin.com/ogika

CSS:

.month.hide { display: none; }

HTML

<p><a href="#" class="month-next">Next Month</a></p>

<table class="month"><tr><td>a</td></tr></table>
<table class="month hide"><tr><td>b</td></tr></table>
<table class="month hide"><tr><td>c</td></tr></table>
<table class="month hide"><tr><td>d</td></tr></table>

JavaScript:

$(function () {
    $(".month-next").click(function () {
        var months = $(".month"), numMonths = months.length, curr = 0;
        return function () {
            //Hide the current table, by adding the 'hide' class
            months.eq(curr).addClass("hide"); 

            //Get the index of next table in the list
            curr = (curr + 1) % numMonths;  

            //Show the next table, by removing the 'hide' class
            months.eq(curr).removeClass("hide"); 
        }
    }());
});

In the above code, months is the list of tables...which in this case, holds 4 elements; and numMonths is the number of elements...ie, 4.

The 'magic' of the above code is this line: curr = (curr + 1) % numMonths;

That line gets the index of the next element to be displayed, and it works in a circular fashion.

Let's take an example:

   0     1     2     3
=========================
|  a  |  b  |  c  |  d  |
=========================

Now, let's say curr is 0:

   0     1     2     3
=========================
|  a  |  b  |  c  |  d  |
=========================
   ^

//curr is currently 0
curr = (curr + 1) % numMonths; //(0 + 1) % 4
//now, curr is 1

   0     1     2     3
=========================
|  a  |  b  |  c  |  d  |
=========================
         ^

After that line of code is executed, curr becomes 1: (0 + 1) % 4 = 1. This means that the next index of the element to be displayed is 1. Therefore, we get the next element and show it, by removing the hide class: months.eq(curr).removeClass("hide");

Now let's look at the example where curr is the last element: 3

   0     1     2     3
=========================
|  a  |  b  |  c  |  d  |
=========================
                     ^

//curr is currently 3
curr = (curr + 1) % numMonths; //(3 + 1) % 4
//now, curr is 0

   0     1     2     3
=========================
|  a  |  b  |  c  |  d  |
=========================
   ^

As you can see, curr is now 0...and thus the cycle continues.

Andreas Grech