All Posts by Email, Once a Week

Media Temple logo

CSS-Tricks is brought to you in part by Media Temple, the web hosting that we both use and recommend.

Display Only First X Divs, Toggle Rest

Last updated on:
var news = 2;
hidenews = "- Hide news archive";
shownews = "+ Show news archive";

$(".archive").html( shownews );

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


<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


  1. Pol
    Permalink to comment#

    sweet, thank you!

  2. Kevin
    Permalink to comment#

    Excellent, thanks for this =)

  3. Simon

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

  4. sdf
    Permalink to comment#

    first post sample piece of code.

Leave a Comment

Posting Code

We highly encourage you to post problematic HTML/CSS/JavaScript over on CodePen and include the link in your post. It's much easier to see, understand, and help with when you do that.

Markdown is supported, so you can write inline code like `<div>this</div>` or multiline blocks of code in in triple backtick fences like this:

  function example() {
    element.innerHTML = "<div>code</div>";

There's a whole bunch of content on CSS-Tricks.

Search for Stuff   •   Browse the Archives

Get the Newsletter ... or get the RSS feed