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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:34:35+00:00 2026-05-27T10:34:35+00:00

I have setup a sidebar via the jQuery API. Now I noticed that when

  • 0

I have setup a sidebar via the jQuery API. Now I noticed that when I refresh the page, the sidebar is going to close itself automatically.

I found a cookie thing on the internet but for some reason it does not work with my sidebar script.

What am I doing wrong?

$(document).ready(function() {
    var sidebar_status = $.cookie("sidebar_status");

    if (sidebar_status == null) {
        $("a#ToggleSidebar").addClass('closed');
        $("#sidebar").hide();
    };

    if (sidebar_status == "closed") {
        $("a#ToggleSidebar").removeClass('open');
        $("a#ToggleSidebar").addClass('closed');
        $("#sidebar").css("display", "none");
    };

    if (sidebar_status == "open") {
        $("a#ToggleSidebar").addClass('open');
    };


    $("a#ToggleSidebar").click(function() {
        if ($("a#ToggleSidebar").attr("class") == 'open') {
            $(this).removeClass('open');
            $(this).addClass('closed');
            $.cookie("sidebar_status", "closed", {
                path: '/',
                expires: 100
            });
            $("#sidebar").animate({
                width: 'hide',
                opacity: 'hide'
            }, 'slow');
        } else {
            $(this).removeClass('closed');
            $(this).addClass('open');
            $.cookie("sidebar_status", "open", {
                path: '/',
                expires: 100
            });
            $("#sidebar").animate({
                width: 'show',
                opacity: 'show'
            }, 'slow');
        }
    });

EDIT:

There is something like a button <a href="#"></a> which should open a <div></div>. Now, I expect that when somebody visits the page the first time, that the <div></div> is closed. When the user clicks on the <a href="#"></a> it opens the <div></div> and saves a cookie. When the user should now refresh the page, the <div></div> should stay open until the user uses the <a href="#"></a> to close it again. That is what I can not get to work.

  • 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-27T10:34:35+00:00Added an answer on May 27, 2026 at 10:34 am

    Comment from OP: Now the Sidebar opens and a cookie is saved on the computer, when I however try to close the sidebar again, nothing happens.

    For jQuery 1.6 and up, this…

    if ($("a#ToggleSidebar").attr("class") == 'open') {
    

    should be this (replace attr with prop)…

    if ($("a#ToggleSidebar").prop("class") == 'open') {
    

    But this is best…

    if ($("a#ToggleSidebar").hasClass("open")) {
    

    This…

    $(this).removeClass('open');
    $(this).addClass('closed');
    

    ….can simply be written as this (it’s called chaining)…

    $(this).removeClass('open').addClass('closed');
    

    While we’re at it…

    This:

    if (sidebar_status == null) {
        $("a#ToggleSidebar").addClass('closed');
        $("#sidebar").hide();
    };
    
    if (sidebar_status == "closed") {
        $("a#ToggleSidebar").removeClass('open');
        $("a#ToggleSidebar").addClass('closed');
        $("#sidebar").css("display", "none");
    };
    
    if (sidebar_status == "open") {
        $("a#ToggleSidebar").addClass('open');
    };
    

    …can be written like this….

    if (sidebar_status == null) {
        $("a#ToggleSidebar").addClass('closed');
        $("#sidebar").hide();
    } else if (sidebar_status == "closed") {
        $("a#ToggleSidebar").removeClass('open').addClass('closed');
        $("#sidebar").hide();
    } else if (sidebar_status == "open") {
        $("a#ToggleSidebar").addClass('open').removeClass('closed');
    };
    

    (jQuery .hide() is equivalent to jQuery .css("display", "none");)

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

Sidebar

Related Questions

I have setup svnserve and for now now I am testing a repository with
I have the following ul setup: <div id=sidebar> <ul id=themenu class=sf-menu sf-vertical> <li> <a
I have built a sidebar navigation that collapses & expands when the nav titles
I have setup a basic WCF client/server which are communicating via Named pipes. It
I have setup the following jQuery UI Dialog within my document.ready() section, i.e: $(#dialog).dialog({
I have a draggable that is connected via connectToSortable to multiple sortables. I want
I am trying to create a jquery function where I have a sidebar on
I have setup a WCF service that i am running on IIS 7 that
I have setup my versioned API like this with only a small tweak for
I have an admin setup that includes a lookup that spans a relationship, school__year

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.