MR.ABC
MR.ABC

Reputation: 4862

jQuery calculation inside loop

How can i make a loop and add some values for the next iteration together like these in jQuery ?

var currLength = length;
for (var i = 1; i < dotCount -1; i++) {
  $("#myElement" + i).css("width", currLength);
  currLength += length + margin;
}

Edit: These is just an example. I just wanted to know how to use the .each function of jQuery because i had some different idea of usage. I doesn't make any difference in relation to a for loop

Upvotes: 1

Views: 144

Answers (2)

MR.ABC
MR.ABC

Reputation: 4862

These is the concrete scenario how i end up. Just need an idea of .each function, because i was thinking there is a way to merge these loops together.

        var bCurrLeft = left;
        $(this.backgroundElements).each(function()
        {
            $(this).animate(
                {
                    left: bCurrLeft,
                    top: top
                }, moveSpeed);
            bCurrLeft += singleWidth  + singleMargin;
        });
        var fCurrLeft = left;
        $(this.foregroundElements).each(function()
        {
            $(this).animate(
                {
                    left: fCurrLeft,
                    top: top
                }, moveSpeed);
            fCurrLeft += singleWidth  + singleMargin;
        });

        $(this.allElements).promise().done(callback);

Upvotes: 0

iambriansreed
iambriansreed

Reputation: 22241

Shouldn't it be:

var currLength = length;
for (var i = 1; i < dotCount -1; i++) {
  $("#myElement" + i).css("width", currLength);
  currLength += margin;
}

currLength is set to the length already, right?

Upvotes: 1

Related Questions