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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:04:09+00:00 2026-05-22T23:04:09+00:00

I’ve couple of resizable objects and I’d like to trigger function ‘changeS()’ when I

  • 0

I’ve couple of resizable objects and I’d like to trigger function ‘changeS()’ when I stop resizing the object.
Function changeS() should takes as first argument the name of the object for which it’s called.
I mean:
when I stop resizing object “#resizable0” then should be called changeS(#resizable0,new_size – old_size),
when I stop resizing object “#resizable1” then should be called changeS(#resizable1,new_size – old_size), etc.

There is everything OK when I just set in code this method for every object i have, but I’d like to do it in loop for(), because there will be much more objects, and then I have a problem:

var old_size=0;
var new_size=0;
var x,i;

for(i=0; i<5; i++){
x = "#resizable"+i;
var $res = $(x);

$res.resizable({
    grid: 22, minHeight:22, handles: 's',
    start: function(event,ui) {
        old_size = ui.originalSize.height;
    },
    stop: function(event,ui) {
        new_size = ui.size.height;

        if(new_size!=old_size)
            changeS($res,new_size - old_size);
    }
});
}

I don’t know how to correctly pass a name of object as an argument of the function.
When function changeS() is executed it’s always called with the same name – name of the last created object (“#resizable5” – in this case).

My question is:
How do I make for each object the function changeS() was called with the corresponding argument(name of this object)?

Thanks, popKonr

  • 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-22T23:04:10+00:00Added an answer on May 22, 2026 at 11:04 pm

    The functions you’re creating in the loop are closures. They keep an enduring reference to the variables they close over, not a copy of the value as of when the function is defined. And so all of them see the last value you assign to $res, not the value during “their” loop.

    The usual way to solve this is to use a factory function, something like this:

    var x,i;
    
    for(i=0; i<5; i++){
        x = "#resizable"+i;
        var $res = $(x);
    
        makeResizeable($res);
    }
    
    function makeResizeable($thisres) {
        var old_size = 0,
            new_size = 0;
    
        $thisres.resizable({
            grid: 22, minHeight:22, handles: 's',
            start: function(event,ui) {
                old_size = ui.originalSize.height;
            },
            stop: function(event,ui) {
                new_size = ui.size.height;
    
                if(new_size!=old_size)
                    changeS($thisres,new_size - old_size);
            }
        });
    }
    

    Note how we now use $thisres, the argument passed into the factory function makeResizeable, rather than $res, since the argument baked into the functions we create within makeResizeable isn’t changed. Also note I’ve moved new_size and old_size into that factory function as well. The event handlers will close over that data, and so each pair of them will get their own private copies of those variables.

    Closures are not complicated, but there are a few fundamental things about them people tend to misunderstand. Once you’ve got those down, though, you’ll be in good shape.

    • 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
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
i got an object with contents of html markup in it, for example: string
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I
I would like to count the length of a string with PHP. The string
I'm making a simple page using Google Maps API 3. My first. One marker

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.