Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7801737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:02:18+00:00 2026-06-02T01:02:18+00:00

How do I remove all class divs except the last 2 using jquery? I

  • 0

How do I remove all class divs except the last 2 using jquery?

I have a set of comments that show in a .comment_container class. When show all comments is clicked a list of all comments for a particular micropost is shown before the most 2 recent which show by default.

How ever if a user types a comment before clicking to see all comments this comment is added to the list and now 3 recent comments show. Because of the way I’ve set up some back end code it causes the least most recent comment to be shown twice when the show all link is clicked.

No need to get into the details but what I’ve decided to do is count the total divs and from the total remove all the divs but 2 (the last 2 in the list which are the most recent).

How can I achieve this using jquery?

Kind regards

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T01:02:19+00:00Added an answer on June 2, 2026 at 1:02 am

    Use slice() (and credit to @Felix Kling for the update):

    $(".comment_container").slice(0, -2).remove();
    

    However, given your description of your problem this doesn’t sound like the best course of action. It may be worth you starting a new question about the problem you have with the comments with some detail about your code.

    More info on slice().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some buttons that when clicked, invoke jQuery to remove and replace their
I have a function that finds all the DIVS with a class of boxes
I'd like to remove all default values that have been setup in a particular
I have a set of divs that represent story categories. In each category, there's
I have html page and I need remove all div except the div with
In jQuery: Scenario ** - I have four Divs that have hover effects. -
I'm writing an immutable binary tree class where all of the methods (Insert, Remove,
One can remove all calls to printf() using #define printf . What if I
I want to remove all vba-modules from an MS Word template using VBScript. I
I have divs that are initially closed. The code below then controls the opening/closing:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.