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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:55:01+00:00 2026-06-07T11:55:01+00:00

What is the correct way to make the following code reusable (so that I

  • 0

What is the correct way to make the following code reusable (so that I don’t have to have so many versions of the same function each time that I want to hide/show a set of DIVs):

// instructions
$('#hideInstructionsOverview').click(function() {
    $('#instructionsOverview').fadeOut(400, function () {
        $('#instructionsDetail').fadeIn(400);
    });
});

$('#hideInstructionsDetail').click(function() {
    $('#instructionsDetail').fadeOut(400, function () {
        $('#instructionsOverview').fadeIn(400);
    });
});

// group
$('#hideGroupOverview').click(function() {
    $('#groupOverview').fadeOut(400, function () {
        $('#groupDetail').fadeIn(400);
    });
});

$('#hideGroupDetail').click(function() {
    $('#groupDetail').fadeOut(400, function () {
        $('#groupOverview').fadeIn(400);
    });
});

The above code is used in the same way to hide “instructions” and “group” divs (and about 7 other divs in the same manner). The similar way that each is called is with this code:

<a id="hideInstructionsOverview" href="javascript:void(0);"><img src="img/misc/plus.png" alt="" width="40" height="40" border="0"></a>
<a id="hideInstructionsDetail" href="javascript:void(0);"><img src="img/misc/minus.png" alt="" width="40" height="40" border="0"></a>

<a id="hideGroupOverview" href="javascript:void(0);"><img src="img/misc/plus.png" alt="" width="40" height="40" border="0"></a>
<a id="hideGroupDetail" href="javascript:void(0);"><img src="img/misc/minus.png" alt="" width="40" height="40" border="0"></a>

I’m thinking that there’s a way that I can create a function that accepts variables, and that I pass three variables on click: (1) what image id was clicked (I think that $this is probably used), (2) what div to fadeOut and (3) what div to fadeIn. I can’t seem to get the variable creation and passing correct, but I imagine that the final, reusable function would look something like this:

// divToggler
$('$this').click(function() {
    $('#divToFadeOut').fadeOut(400, function () {
        $('#divToFadeIn').fadeIn(400);
    });
});

Thanks so much in advance!

Berklie

  • 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-07T11:55:03+00:00Added an answer on June 7, 2026 at 11:55 am

    Ideally you would tackle this hierarchically rather than relying on lots of IDs. This is not always possible, and since you didn’t post your HTML structure, I’ll just answer the question at hand. Note, this utilises ECMA 5’s Object.keys() method. It would need modifying for use in IE <= 8.

    var map = {
        hideInstructionsOverview: {out: 'instructionsOverview', in: 'instructionsDetail'},
        hideInstructionsDetail: {out: 'instructionsDetail', in: 'instructionsOverview'}
        /* etc... */
    }
    
    $('#'+Object.keys(map).join(', #')).on('click', function() {
        var thisMapEntry = map[$(this).attr('id')];
        $('#'+thisMapEntry.out).fadeOut(400, function () {
            $('#'+thisMapEntry.in).fadeIn(400);
        });
    });
    

    It could be made shorter still with some assumptions about naming conventions (which appear sound, from what you posted) but I won’t risk that one.

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

Sidebar

Related Questions

The answer to What is the correct way to make exceptions serializable? says that
All, I have the following code to figure out a time based on milliseconds
I have this following code: if (userValueSom01 == realResult01) { //answer = correct //count
I have the following code, which modifies each line of the file test.tex by
Referring to What is the correct way to make a custom .NET Exception serializable?
I have the following code implementation of Breadth-First search. trait State{ def successors:Seq[State] def
I have the following code: $sortorder = $_GET['sort']; switch($sortorder) { case modulea: $result2 =
I have several controllers that require a correct user for their edit/update/delete actions. What
I'm playing around with Ruby and I have written the following code: module IdAndNameRedefine
Could you guys please tell me how I can make the following code more

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.