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

  • SEARCH
  • Home
  • 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 8226287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:42:42+00:00 2026-06-07T15:42:42+00:00

Hi I got help and eventually I got jquery show/hide function that work well.

  • 0

Hi I got help and eventually I got jquery show/hide function that work well.
But I spend a whole day to do next step by make it work when there are several comment groups.

the code here

var toggle = false;
$(function() {
    $(document).on('click', 'a.comments', function(e) {
        var $this = $(this);
        $('.toggleComments').toggle(1000,function() {
            if(!toggle) {
                $this.text('Hide Comments');
                toggle = !toggle;
            }else {
                $this.text('Show Comments');
                toggle = !toggle;
            }
        });
        e.preventDefault();
    });
});

<a href="#" class="comments">Show Comments</a><br />
<div class="toggleComments" style="display:none;">This is #comment1 <br />This is #comment2 <br /></div>
<a href="#" class="comments">Show Comments</a><br />
<div class="toggleComments" style="display:none;">This is #comment1 <br />This is #comment2 <br /></div>
<a href="#" class="comments">Show Comments</a><br />
<div class="toggleComments" style="display:none;">This is #comment1 <br />This is #comment2 <br /></div>
<a href="#" class="comments">Show Comments</a><br />
<div class="toggleComments" style="display:none;">This is #comment1 <br />This is #comment2 <br /></div>

There are 4 comment groups but because of the html DOMs are the same then they could not working independently while click each of them.

Can you please advise how to deal with this situation manage them show/hide independently without having assigned jquery script per comments group?

  • 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-07T15:42:43+00:00Added an answer on June 7, 2026 at 3:42 pm

    Working demo http://jsfiddle.net/ZXNWF/ or http://jsfiddle.net/X5ZUU/1/ or with text change: http://jsfiddle.net/Xam9q/

    API: http://api.jquery.com/nextAll/

    and then using nextAll always get the :first one to show.

    rest feel free to play around with the demo, hope this help for the cause.

    code

    var toggle = false;
    $(function() {
    
        $(document).on('click', 'a.comments', function(e) {
             $('.toggleComments').hide();
            var $this = $(this);
            $(this).nextAll('.toggleComments:first').toggle(1000,function() {
                if(!toggle) {
                    $this.text('Hide Comments');
                    toggle = !toggle;
                }else {
                    $this.text('Show Comments');
                    toggle = !toggle;
                }
            });
            e.preventDefault();
        });
    });
    ​
    

    or simple

    var toggle = false;
    $(function() {
    
        $(document).on('click', 'a.comments', function(e) {
            $('.toggleComments').hide();
            var $this = $(this);
             $('.comments').text('Show Comments');
                $this.text('Hide Comments');
            $(this).nextAll('.toggleComments:first').toggle(1000,function() {    
            });
            e.preventDefault();
        });
    });
    ​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got help with correct calculation the other day but I've hit a block
I got help earlier for the placement if my jquery box but it wasn't
I've got an app with a big 'help' page that describes how to use
I asked a question yesterday on here and got some awsome help, but I
I need help in designing a Chess game. I've already started but haven't got
I recently got help on When select value from combo, highlight that record in
I've got an internationalised web application in which all the text that eventually gets
i just recently asked a question and got help but i have another problem
I just got help with the jQuery portion of this code (thanks David Thomas)
I have a query which I got help with last night but I am

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.