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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:06:59+00:00 2026-05-23T19:06:59+00:00

I want to do a very simple expand/collapse using JavaScript where when I click

  • 0

I want to do a very simple expand/collapse using JavaScript where when I click “Title”, it shows “Text” (which is hidden when the page first loads). Furthermore, when I click on the “Title” again, I want “Text” to get hidden again.

<div>
    <h1>Title</h1>
        <p>Text</p>
</div>

I’ve never used jQuery or JavaScript before so it would be great if you can explain just a little bit about how this toggle code works. Thanks.

  • 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-23T19:06:59+00:00Added an answer on May 23, 2026 at 7:06 pm

    Well this should do it:

    $(function() {
    
        $('h1').click(function() {
            $(this).next('p').toggle();
        });
    
    });
    

    So now let me explain the code.

    The first line is accepting a function that will run when the document has finished loading. It is equivalent to:

    $(document).ready(function() {
        // Stuff to do when the document is ready.
    });
    

    The code in the middle says, for every h1 element, when it gets clicked, execute a function that finds the p element next to it and toggle its visibility. The this in the first selector refers to the actual <h1> DOM element. I did it this way so if you had a structure like the following, the toggling would only affect the content next to the paragraphs.

    <div>
        <h1>Section 1</h1>
        <p>Section 1 Content</p>
    </div>
    <div>
        <h1>Section 2</h1>
        <p>Section 2 Content</p>
    </div>
    

    Cheers!

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

Sidebar

Related Questions

I want to create a very simple spell checker using a text file with
I want to write a very simple script , which takes a process name
I want to create a very simple HTML/AJAX based GUI for a Python program.
I want to develop a very simple 2D game in Python. Pygame is the
I want to write a very simple Spell Checker. The spell checker will try
Very simple Winforms application I want to deploy manually. Can all the referenced assemblies
I'll be very simple: I want to be able to store Java objects from
What I want to do is very simple but I'm trying to find the
I have a very simple greasemonkey script that I want to call an already
I'm creating a very simple django upload application but I want to make it

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.