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 6385807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:56:45+00:00 2026-05-25T02:56:45+00:00

I have a problem to show and hide some div. The only thing i

  • 0

I have a problem to show and hide some div.
The only thing i have is a span with a class. Where we need to click on, to show or hide the div below the parent h2
I can’t add some classe’s to the h2 or div (this come from some cms, where we can’t add a css class), so we need to do it with some jquery.

<div>
  <h2>
    <span class="h2toggle">Heading (This shows an hide the div below)</span>
  </h2>
  <div>
    <p>Some text</p>
    <p>More text</p>
  </div>
</div>

<div>
  <h2>
    <span class="h2toggle">Heading 2 (This shows an hide the div below)</span>
  </h2>
  <div>
    <p>Some text 2</p>
    <p>More text 2</p>
  </div>
</div>  

Now i have something like the following code.
But now, when the page will be loaded. The div around “some text” & “more text” will we hidden.
But you can see that it is not hidden from the beginning. It’s sliding up.

It should be hidden ( the div with text ) from the beginning. Without seeing any change to the page.
After that the page is loaded, it must be possible to toggle on the headings.

(function ($) {
$(function () {
    $('.h2toggle').each(function () {
        $(this).parent('h2').addClass('h2toggle');
        $(this).removeClass('h2toggle');
        $(this).parent().siblings(':not(h2):visible').slideUp()
    });

    $('.h2toggle').click(function () {
        $(this).siblings(':not(h2):visible').slideUp()
        $(this).siblings(':not(h2):hidden').slideDown()
    });
});
})(jQuery);
  • 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-05-25T02:56:45+00:00Added an answer on May 25, 2026 at 2:56 am

    demo using ternary operators:

    DEMO FIDDLE

    And it will act as a beautiful Accordion! (If other content is opened.. close it!)

    $('.h2toggle').parent().next('div').hide();
    $('.h2toggle').click(function() {
        var d = $(this).parent('h2').next('div');
     check = (d.is(':visible')) ?
             (d.slideUp()) :
             ($('.h2toggle').parent('h2').next('div').slideUp()) (d.slideDown());
    });
    

    In this case, how ternary operators work:

    _____statement___    __is is true_      __________________is false______________________________________________
    |                |   |            |    |      (do that)                                          (and that too) |
    (d.is(':visible')) ? ( d.slideUp() ) : ( $('.h2toggle').parent('h2').next('div').slideUp() )   ( d.slideDown() );
    

    … And you’ll get the smallest script to get an accordion!
    Happy coding!

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

Sidebar

Related Questions

I have some nested tables that I want to hide/show upon a click on
I need a js script show / hide with only shown one div at
I have a nested function to show/hide paragraphs news-ticker-style. The problem is that when
I have a question about usability/design. I currently am using some JQuery to hide/show
I have explained the problem below this code <div id='content'> <div id='help'> blah blah
I have a form with some select lists that will show/hide more input fields
In http://albertanglada.es I have this: $('#ultimas,#populares').click(function(e) { e.preventDefault(); $('.grupo').hide(); var id= 'div' + $(this).attr('id');
I have problem in some JavaScript that I am writing where the Switch statement
I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE
I have problem when I try insert some data to Informix TEXT column via

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.