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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:52:37+00:00 2026-05-31T00:52:37+00:00

Question Edited: So, probably i didn’t asked the question in the best way: With

  • 0

Question Edited:

So, probably i didn’t asked the question in the best way:

With X being the class name from the divs:

function functionName(X) {
        $('h2.X').click(function(){
    $(this).parent().find('div.X').slideToggle("slow");
});

    <h2 class="divName" onclick="functionName(X)">Title</h2>
    <div class="divName">
    <p> description </p>
    </div>

    <h2 class="divName2" onclick="functionName(X)">Title</h2>
    <div class="divName2">
    <p> description </p>
    </div>

Well, for those who don’t understand, just don’t downvote. Thanks a lot :/

Let’s put it this way:

 $('h2.divName').click(function(){
        $(this).parent().find('div.divName').slideToggle("slow");
 });

        <h2 class="divName">Title</h2>
        <div class="divName">
        <p> description </p>
        </div>


        <h2 class="divName2">Title</h2>
        <div class="divName2">
        <p> description </p>
        </div>

        <h2 class="divName3">Title</h2>
        <div class="divName3">
        <p> description </p>
        </div>

I want to replicate the accordeon to function with all the divs…

  • 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-31T00:52:38+00:00Added an answer on May 31, 2026 at 12:52 am

    I suggest you have a good read of the documentation for jQuery as I think you are missing the point.

    Given the following markup:

    <h2 class="accTrigger">Title</h2>
    <div class="accTarget">
        <p> description </p>
    </div>
    <h2 class="accTrigger">Title</h2>
    <div class="accTarget">
        <p> description </p>
    </div>
    

    You could write something like this:

    $("h2.accTrigger").click(function(event){
        $(this).next("div.accTarget").slideToggle();
    });
    

    What you are doing here is creating an event handler for all h2 elements that trigger the visibility of the next div.

    This would allow you to do what you want whilst keeping Javascript out of your page which is very much what you should strive to do.

    The recommended way to do this now though would be to use event delegation and reduce the number of event handlers and therefore memory usage.

    Rewriting your markup as follows:

    <div class="accordion">
        <h2>Title</h2>
        <div>
            <p> description </p>
        </div>
        <h2>Title</h2>
        <div>
            <p> description </p>
       </div>
    </div>
    

    Will allow you to write code like this:

    $("div.accordion").on("click", "h2", function(event){
        $(this).next("div").slideToggle();
    });
    

    Which is really simple to write. What’s going on here is that jQuery is creating only one eventHandler per each instance of a div with the class accordion on the page.

    That eventHandler is listening for any click events bubbling up from its children and if one of them is a h2 element then it will pass the context of that function to that element and toggle the visibility of that next div.

    It’s really important to try to understand what you are doing when you are writing Javascript and really learn the basics. Javascript is the most important language in the web and should be treated as a first class language.

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

Sidebar

Related Questions

Edited Question: This should be clear. using System; namespace UpdateDateTimeFields { class Program {
I edited the question so it would make more sense. I have a function
Warning: This question has been heavily edited. I tried my best to guess the
I edited the question after David Hanak's answer (thanks btw!). He helped with the
I edited this question after i found a solution... i need to understand why
I've edited this quite a bit and bolded my question at this point. I
This is probably a completely stupid question, but i'm pretty new at objective-C and
I have asked the same question before and I found this solution : if
I am in the process of writing a class that will probably end up
Edit: Changed question title from Does C# allow method overloading, PHP style (__call)? -

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.