treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Display Only First X Divs, Toggle Rest

Last updated on:

var news = 2;
hidenews = "- Hide news archive";
shownews = "+ Show news archive";

$(".archive").html( shownews );
$(".news:not(:lt("+news+"))").hide();

$(".archive").click(function (e) {
   e.preventDefault();
       if ($(".news:eq("+news+")").is(":hidden")) {
           $(".news:hidden").show();
           $(".archive").html( hidenews );
       } else {
           $(".news:not(:lt("+news+"))").hide();
           $(".archive").html( shownews );
       }
});

HTML:

<div class="news">First news</div>
<div class="news">Second news</div>
<div class="news">Third news</div>
<a class="archive" href="#"></a>

Reference URL

View Comments

Comments

  1. Permalink to comment#

    sweet, thank you!

  2. Permalink to comment#

    Excellent, thanks for this =)

  3. Sorry, the reference URL has not been available for a long time. It’s fixed now!

Leave a Comment

Use markdown or basic HTML and be nice.