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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:24:57+00:00 2026-06-07T15:24:57+00:00

I am using the toggle() method on buttons so that they can have a

  • 0

I am using the toggle() method on buttons so that they can have a do something / revert changes behaviour. For instance:

$('button').toggle(function() {
  // case 1: maximize
  // ...
  another_function($(this));
  }, function() {
   // case 2: minimize
   // ...
  another_function($(this));
});

In both toggle cases, the same function is called at the end (another_function()). I would like from that function to find out whether we are in case 1 or in case 2 without having to manipulate the button myself (e.g add a class, set a custom attribute before calling the function, and check on those changes inside the function).

Is there a way to find out from the element state data itself in which toggle case we’re in (surely jQuery must set a state somewhere to know which part of the toggle to execute)?

PS: there are many posts related to that topic already on SO but they’re about hide/show cases and their suggestions (check if it’s visible, hidden…) doesn’t apply here.

  • 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-07T15:24:59+00:00Added an answer on June 7, 2026 at 3:24 pm

    You already know what state you are in (maximize/minimize) because the code for that particular state is executing. So you could simply pass this information to another_function as an argument:

    $('button').toggle(function() {
      // case 1: maximize
      // ...
      another_function($(this), 'maximize');
      }, function() {
       // case 2: minimize
       // ...
      another_function($(this), 'minimize');
    });
    

    Update: Grovelling jQuery’s internal state

    jQuery currently (1.7.2) uses a private back door into the .data() mechanism to store state for its toggles. This back door is explicitly marked private more than one in the jQuery source and it goes without saying that any part of what follows is subject to change without any notice. Therefore I strongly recommend not going down this path.

    That said, here’s what’s going on:

    jQuery uses a private data space accessed through $._data() to store the current click count for toggles. This click count modulo 2 is used to decide which function to execute and is incremented on each click.

    The key used to store this data is currently the string "lastToggle" + guid, where guid is a value typically auto-generated by jQuery as well (using a global incrementing counter). The value of guid is set on the toggle handler functions and you can retrieve it after setting the toggle like this:

    var f1 = function() {
      // case 1: maximize
      // ...
      another_function($(this), 'maximize');
    },  f2 = function() {
      // case 2: minimize
      // ...
      another_function($(this), 'minimize');
    };
    
    $('button').toggle(f1, f2);
    
    // guid is now available on both handlers:
    console.log(f1.guid, f2.guid);
    

    You can also fix the value of guid by setting it on the first handler before calling toggle:

    var f1 = function() {
      // case 1: maximize
      // ...
      another_function($(this), 'maximize');
    },  f2 = function() {
      // case 2: minimize
      // ...
      another_function($(this), 'minimize');
    };
    
    f1.guid = "foo";
    $('button').toggle(f1, f2);
    
    // guid is now fixed to "foo" and available on both handlers:
    console.log(f1.guid, f2.guid);
    

    Given this handle, you can retrieve the count of clicks so far (and thus predict which handler will be called next) with

    $._data($('button'), "lastToggle" + guid);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using jQuery's .toggle method to create a menu that opens and closes when
I am trying to write a method that will toggle a button from being
I'm using simple jQuery toggle method: $('fieldset').click(function () { $('fieldset>ul').toggle(slow); }); Is there any
How can I toggle two images by fading using jQuery .fadeToggle method.
I'm using jquery and what I'm doing is binding the toggle method to a
I'm using the jQuery toggle function to change some characters of an text input.
I have some elements that, upon hover, toggle the display (none/block) of a child
I have a bunch of partials that I want to render horizontally, using form-inline
I'm trying to create several buttons that have 4-5 frames of animation in them,
I have a list view that can display items based on internal state (it

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.