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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:33:04+00:00 2026-05-13T05:33:04+00:00

I’m currently fighting a fierce battle with an interface I’m trying to put together.

  • 0

I’m currently fighting a fierce battle with an interface I’m trying to put together. I’m pretty decent at CSS & XHTML, but my jQuery/JavaScript skills are not too impressive. What I’m looking for is not so much a strict answer, but some hints on how to go about these kinds of tasks.

What I am trying to achieve is a layout with some boxes in the main-content area that can be expanded and collapsed when the user press either a link in the sidebar or on a header on the box itself.

dashboard http://www.timkjaerlange.com/foobar/example-dashboard-v01.gif

Furthermore I want the browser to remember what box was expanded last by setting a cookie. If that wasn’t enough I also want to be able to add a “limited”-class to each box and thereby disabling it, and it’s corresponding sidebar-list-item.

Also I want the box to have a class based on it’s state (expanded, collapsed, limited).

This is my markup:

<!-- S I D E B A R - L E F T -->    

<div id="sidebar-left">
    <ul class="sidebar-menu-1">
        <li><strong>List headline</strong>
        </li>
        <li><a href="#">Open module 1</a>
        </li>
        <li><a href="#">Open module 2</a>
        </li>
        <li><a href="#">Open module 3</a>
        </li>
    </ul>

</div><!--/sidebar-left-->


<!-- M A I N - C O N T E N T -->    

<div id="main-content">

    <!-- dashboard-module -->
    <div class="dashboard-module">
    <div class="dashboard-module-content limited">
        <h2>Headline 1</h2>
        <p class="teaser">Teaser-text 1</p>
        <div class="expand-collapse">
            [some content]
    ´   </div>
    </div>
    </div><!--/dashboard-module-->


    <!-- dashboard-module -->
    <div class="dashboard-module">
    <div class="dashboard-module-content limited">
        <h2>Headline 2</h2>
        <p class="teaser">Teaser-text 2</p>
        <div class="expand-collapse">
            [some content]
    ´   </div>
    </div>
    </div><!--/dashboard-module-->


    <!-- dashboard-module -->
    <div class="dashboard-module">
    <div class="dashboard-module-content limited">
        <h2>Headline 3</h2>
        <p class="teaser">Teaser-text 3</p>
        <div class="expand-collapse">
            [some content]
    ´   </div>
    </div>
    </div><!--/dashboard-module-->


</div><!--/main-content-->

And this is my jQuery:

// If the dashboard-module is "limited" do this:

$('div[class*=limited]').each( function(index) {
    var countFix = $('div.dashboard-module-content').index(this);
    var countFixPlusTwo = countFix + 2;
    $( 'ul.sidebar-menu-1 li:nth-child(' + countFixPlusTwo + ')' ).unbind('click').removeClass('selected').addClass('sidebar-menu-locked');
    $(this).parent('.dashboard-module').find('h2').unbind('click');
    $(this).find('.expand-collapse').remove();
    $(this).find('.dashboard-module p:hidden').show();
    $(this).addClass('limited-btn');
});

// Check for cookies and do stuff accordingly:

var cookieTest = $.cookie("cookie");

if(cookieTest == null){
    $('.js-hidden').hide();
    $('ul.sidebar-menu-1 li:not(.sidebar-menu-locked):eq(1)').addClass('selected');
    $('.dashboard-module-content:not(.limited):eq(0)' ).toggleClass('hide-btn');    
    $('.dashboard-module-content:not(.limited):not(:eq(0))' ).removeClass('hide-btn');
    $('.expand-collapse:eq(0)' ).show();
    };

if(cookieTest != null){
    var cookieTestPlusTwo = parseInt(cookieTest, 10) + 2;
    $('.dashboard-module-content:eq(' + cookieTest + ')' ).toggleClass('hide-btn'); 
    $('.dashboard-module-content:not(:eq(' + cookieTest + '))' ).removeClass('hide-btn');   
    $('ul.sidebar-menu-1 li:nth-child(' + cookieTestPlusTwo + '):not(.sidebar-menu-locked)').addClass('selected');
    $('ul.sidebar-menu-1 li:not(:nth-child(' + cookieTestPlusTwo + '))').removeClass('selected');
    $('.expand-collapse:eq(' + cookieTest +  ')' ).show();
    $('.expand-collapse:not(:eq(' + cookieTest +  '))').hide();
    };

// When clicking dashboard-module headers do stuff:

$('.dashboard-module h2:not(.limited h2):not(.tooltip h2)').click(function(index) {

    var indexFoo = $('.dashboard-module h2:not(.tooltip h2)').index(this);
    var indexPlusTwo = indexFoo + 2;
    var indexCookie = indexFoo;

    $.cookie("cookie", indexCookie, { expires: 7 });

    $('.dashboard-module-content:eq(' + indexFoo + ')').toggleClass('hide-btn');    
    $('.dashboard-module-content:not(:eq(' + indexFoo + '))' ).removeClass('hide-btn');

    $('ul.sidebar-menu-1 li:nth-child(' + indexPlusTwo + ')').addClass('selected');
    $('ul.sidebar-menu-1 li:not(:nth-child(' + indexPlusTwo + '))').removeClass('selected');

    $('.dashboard-module-content:eq(' + indexFoo + ') .expand-collapse').toggle('fast');
    $('.dashboard-module-content:not(:eq(' + indexFoo + ')) .expand-collapse').hide('fast');

    $('.dashboard-module-content:not(.limited)').each( function(){
        if($(this).parent('.dashboard-module').find('.hide-btn').length == 0){
            $(this).find('p.teaser').show();
        }
        else {
            $(this).find('p.teaser').hide();
        }
    });
});

// When clicking sidebar list-items do stuff:

$('.sidebar-menu-1 li:not(.sidebar-menu-locked)').click(function(index) {

    var indexFoo = ($('.sidebar-menu-1 li').index(this) - 1);
    var indexPlusTwo = indexFoo + 2;
    var indexCookie = indexFoo;

    $.cookie("cookie", indexCookie, { expires: 7 });

    $('.dashboard-module-content:eq(' + indexFoo + ')').toggleClass('hide-btn');    
    $('.dashboard-module-content:not(:eq(' + indexFoo + '))' ).removeClass('hide-btn'); 

    $('ul.sidebar-menu-1 li:nth-child(' + indexPlusTwo + ')').addClass('selected');
    $('ul.sidebar-menu-1 li:not(:nth-child(' + indexPlusTwo + '))').removeClass('selected');

    $('.dashboard-module-content:eq(' + indexFoo + ') .expand-collapse').toggle('fast');
    $('.dashboard-module-content:not(:eq(' + indexFoo + ')) .expand-collapse').hide('fast');

    $('.dashboard-module-content:not(.limited)').each( function(){
        if($(this).parent('.dashboard-module:not(.limited)').find('.hide-btn').length == 0){
            $(this).find('p.teaser').show();
        }
        else {
            $(this).find('p.teaser').hide();
        }
    });

});

As for now it all works in FF, but in IE7+8 it doesn’t. But my question is not so much about how to make it work in IE, but more on how you would go about this kind of task? I suspect that my jQuery is not exactly DRY, but how do I condense it?

How would you write and organize your code? Sorry if this is a too broad and long-winded question, but I’m really interested in how experienced jQuery designers go about challenges like this one.

Update: Thanks to Tatu Ulmanen’s help I’ve managed to put together this: http://timkjaerlange.com/foobar/jquery-test/index.html

  • 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-13T05:33:04+00:00Added an answer on May 13, 2026 at 5:33 am

    Opening modules

    For the sidebar links -> content relation, use rel tags in sidebar that correspond to the main content’s divs’ ID’s, like this:

    <ul id="sidebar">
        <li><a href="#" rel="tab_1">Open tab 1</a></li>
        <li><a href="#" rel="tab_2">Open tab 2</a></li>
        <li><a href="#" rel="tab_3">Open tab 3</a></li>
    </ul>
    
    ...
    
    <div id="content">
        <div class="module" id="tab_1"> ... </div>
        <div class="module" id="tab_2"> ... </div>
        <div class="module" id="tab_3"> ... </div>
    </div>
    

    And then in jQuery, the change of tab is easy to implement:

    $('#sidebar a').click(function() {
        $('#content div.module').hide();
        $('#content div#'+$(this).attr('rel')).show();
        return false;
    });
    

    Module headers

    If you want to have the headers of the modules to be visible always, you can do something like this:

    <div id="content">
        <div class="module" id="tab_1">
            <h2>Module header</h2>
            <p class="module_tools">
                <a href="#" class="module_hide">Hide</a>
                <a href="#" class="module_show">Show</a>
            </p>
            <div class="module_contents">
                ...
    

    And in jQuery:

    $('#sidebar a').click(function() {
        var $visible_module = $('#content div#'+$(this).attr('rel'));
    
        $('#content div.module div.module_contents').hide();
        $('#content div.module div.module_contents a.module_hide').hide();
        $('#content div.module div.module_contents a.module_show').show();
    
        $visible_module.find('div.module_contents').show();
        $visible_module.find('a.module_hide').show();
        $visible_module.find('a.module_show').hide();
    
        return false;
    });
    

    Module show and hide

    Then, to implement the show buttons in modules, I would ‘cheat’ and have them press the corresponding button in sidebar.

    $('#content .module_show').click(function() {
        $('#sidebar a[rel='+$(this).closest('div.module').attr('id')).click();
        return false;
    });
    

    The hide button is easy to implement:

    $('#content .module_hide').click(function() {
        $(this).closest('div.module').find('div.module_contents').hide();
        return false;
    });
    

    Locked modules

    And last thing is to implement the ‘locked’ feature. For that, I would just add a class to the sidebar’s links that tells the current tab is inactive and then modify the modules according to that:

    <ul id="sidebar">
        <li><a href="#" rel="tab_1">Open tab 1</a></li>
        <li><a href="#" rel="tab_2" class="inactive">Open tab 2</a></li>
    

    And in jQuery:

    $('#sidebar a.inactive').each(function() {
        var $target_module = $('#content div#'+$(this).attr('rel'));
        // Remove the links alltogether
        $target_module.find('.module_hide, .module_show').remove();
        return false;        
    });
    

    And the sidebar’s links’ click event must be modified also:

    $('#sidebar a').click(function() {
        if($(this).hasClass('inactive')) return false;
        ...
    

    That covers all the bases not including the cookie save. Hope this gives you some pointers on how you can simplify your code. Note that I haven’t tested any of this, so I cannot guarantee it works. Feel free to ask if you don’t understand something.

    Be sure to use CSS styling to set the initial state of your application, for example hiding all the ‘hide’ links and .module_contents from modules, as they probably are closed by default.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am trying to loop through a bunch of documents I have to put
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I want use html5's new tag to play a wav file (currently only supported
I have a French site that I want to parse, but am running into

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.