Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery show/hide divs and a counter

I have a random amount of DIVs (minimum 1, max of 10)

<div id="container">
<div class="foo">Content</div> <!-- div 1 -->
<div class="foo">Content</div> <!-- div 2 -->
<div class="foo">Content</div> <!-- div 3 -->
<div class="foo">Content</div> <!-- div 4 -->
<div class="foo">Content</div> <!-- div 5 -->
<div class="foo">Content</div> <!-- i need this one hidden -->
<div class="foo">Content</div> <!-- and this one -->
</div>

I want the first 5 divs to be visible (either with .show() or with a class, doesn't matter). Any additional DIVs should be hidden.

I then simulate the "closing" of a div with:

$('.foo').click(function(){
    $(this).fadeOut('slow');    
});

which removes the clicked div, causing all the divs below to move up one. This is my desired effect.

However, I need some logic here.

If I have less than 5 DIVS, the close facility should be disabled. If I have more than 5 DIVs, then when a div is "closed", i want the next "hidden" div to become visible.

I can add IDs to each DIV if required with IDs like "foo1" "foo2" etc.

like image 255
Ross Avatar asked Oct 19 '10 15:10

Ross


People also ask

How to show or hide div using jQuery?

To show and hide div on mouse click using jQuery, use the toggle() method. On mouse click, the div is visible and on again clicking the div, it hides.

How to show a hidden element in jQuery?

The show() Method in jQuery is used to display the hidden and selected elements. Note: This method display the hidden elements which are using CSS display: none property. The elements are not visible whose visibility is hidden.

What does hide () do in jQuery?

jQuery hide() Method The hide() method hides the selected elements. Tip: This is similar to the CSS property display:none. Note: Hidden elements will not be displayed at all (no longer affects the layout of the page). Tip: To show hidden elements, look at the show() method.

How to show hide a hidden input form field using jQuery?

In jQuery to set a hidden field value, we use . val() method. The jQuery . val() method is used to get or set the values of form elements such as input, select, textarea.


1 Answers

Something like this should work:

$("#container .foo:gt(4)").hide();

$("#container").delegate(".foo", "click", function() {
    if(!$("#container .foo:hidden").length) return;
    $(this).fadeOut('slow', function() { 
        $(this).siblings(":hidden:first").fadeIn()
               .end().remove();
    });
});

You can test it out here. What this does is hides all past 5 using the :gt(4) (0-based) selector. Then we're using .delegate() for efficiency (though a .click() would work too). If there aren't any more hidden, there's no effect. If there are more hidden, fade out the one we clicked, at at the end of the fade show the :first :hiddden one, and .remove() the one we faded out completely.

like image 166
Nick Craver Avatar answered Oct 11 '22 06:10

Nick Craver