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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T12:22:09+00:00 2026-06-02T12:22:09+00:00

trying to toggle a div to expand and contract, it is basically a menu,

  • 0

trying to toggle a div to expand and contract, it is basically a menu, with subnav. so i initially set height to 48px to hide subnav and in case there is no JS it will still look decent, I then assign a toggle to open and close the div (in this case set the height)

it keeps refreshing and automatically opening the div though. doesn’t want to stay open, just expands, gives alert, then closes again.

jQuery(document).ready(function($) { 
$('#nav-wrapper').css('height','48px');
  $('#menu-item-18').click(function() {
    var open = false;
    if(isOpen) {
      $('#nav-wrapper').animate({ height: '-=44' }, 0, function() {}); 
      var isOpen = false;
      alert ('not open')
    } else { 
      $('#nav-wrapper').animate({ height: '+=44' }, 0, function() {}); 
      isOpen = !isOpen; 
      alert ('open') 
    };
  });
}); 

might be way overcomplicating this. I’m sure there is a simpler solution too.

  • 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-02T12:22:13+00:00Added an answer on June 2, 2026 at 12:22 pm

    isOpen is defined in local scope and so the value will be always false. You need to move it outside click handler and then you just need isOpen = !isOpen; to toggle value. see below,

      $('#nav-wrapper').css('height','48px');
      var isOpen = false;
    
      $('#menu-item-18').click(function() {
        isOpen = !isOpen; 
        if(isOpen) {
          $('#nav-wrapper').animate({ height: '-=44' }, 0, function() {}); 
          alert ('not open')
        } else { 
          $('#nav-wrapper').animate({ height: '+=44' }, 0, function() {}); 
          alert ('open') 
        };
      });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to hide/show/toggle a div when a button is clicked. I am
Im trying to toggle an animation on height when a button Show Details/Hide Details
I am trying to toggle text when I click to expand/collapse a div. I
I am trying to toggle the height of div that can vary in height.
I've got some code that uses toggle to show and hide a div, but
I'm trying to expand and collapse a <div> by clicking some text inside the
I'm trying to use next() to toggle a div. The problem is that the
I'm trying to toggle the build_files_toggle classed div in the next row when you
I'm trying to toggle the visibility of certain DIV elements on a website depending
I'm trying to use jQuery to toggle a div if another div does not

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.