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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:28:49+00:00 2026-05-30T20:28:49+00:00

I want to dynamically resize an element upon clicking and have the ability to

  • 0

I want to dynamically resize an element upon clicking and have the ability to revert back to its original size upon a second click. I thought toggleClass could do the job, but apparently it didn’t:

$(document).ready(function() {
 $(this).click(function() {
  var new_size = // dynamically calculated value;
  $('.resize').css({'width': new_size, 'height': new_size});
  $(this).toggleClass('resize');
 });
});

Is there an easy way to work around this?

  • 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-30T20:28:50+00:00Added an answer on May 30, 2026 at 8:28 pm

    This line of code:

    $('.resize').css({'width': new_size, 'height': new_size});
    

    isn’t doing what you think it is. For your object to change size when you add a class, you need an actual CSS rule that specified the .resize class which won’t work with a dynamically calculated size.

    This line of code is just setting a height and width on any object with the .resize class and isn’t doing anything to objects without that class. Thus, you could resize the object once (when it had the class) and never change it’s size again. It won’t toggle anything.

    What I would suggest is that you save the old size and then you can restore it when you want to and also save the toggle value. jQuery’s .data() functions work well for saving this type of info:

    $(document).ready(function() {
        $(this).click(function() {
            var this$ = $(this);
            // if it's currently at the dynamic size, restore the original size
            if (this$.data("dynamicallySized")) {
                $(this.css(this$.data("origSize"))
                   .data("dynamicallySized", false);   // set flag that we're not dynamic
            } else {
                // save original size
                this$.data("origSize", {height: this$.height(), width: this$.width()})
                  .css(new_size)
                  .data(dynamicallySized", true);    // set flag that we're dynamically sized
            }
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to dynamically resize the canvas size according to the content of a
I have a main container div that I want to resize dynamically. In a
I have a custom control and I want to dynamically insert a link to
I have a scenario where I want to dynamically add words of text to
I'm trying to figure out how to have a div dynamically resize, based on
I have a scenerio where I want to dynamically render a custom form object.
I am loading images from a database and want to dynamically resize them according
I want to dynamically hide/show some of the columns in a NSTableView, based on
I want to dynamically get the name of the current stored procedure inside itself
I want to dynamically create object of HtmlDivElement in my jsf managed bean and

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.