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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:57:35+00:00 2026-05-22T12:57:35+00:00

I have a function like so: var eatLunch = (function () { var sandwiches

  • 0

I have a function like so:

var eatLunch = (function () {
    var sandwiches = [$("#sand1"), $("#sand2")];

    return function (eaten) {
        for (var i = 0, len = eaten.length; i < len; i++) {
            sandwiches[i].attr("class", "eaten-by " + eaten[i].by)
        }
    };
}());

eatLunch([
    {
        result: "good",
        by: "Adam"
    },
    {
        result: "meh",
        by: "Adam"
    }
]);

The intention is to cache the dom elements #sand1 and #sand2 in a closure to reduce the number of dom touches this function has to make. Without closure, the elements have to be assigned to variables every time I call the function.

However, the attr method does not seem to work. Upon further inspection, sandwiches[i] is defined, and it has the attr method. The function does not throw an error, it just keeps looping, but the dom elements do not get updated.

If I move sandwiches into the local scope, it works perfectly, but this is more expensive. See below:

var eatLunch = function (eaten) {
    var sandwiches = [$("sand1"), $("sand2")];

    for (var i = 0, len = eaten.length; i < len; i++) {
        sandwiches[i].attr("class", "eaten-by " + eaten[i].by)
    }
};

eatLunch([
    {
        result: "good",
        by: "Adam"
    },
    {
        result: "meh",
        by: "Adam"
    }
]);

What’s going on 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-05-22T12:57:36+00:00Added an answer on May 22, 2026 at 12:57 pm

    You’re probably running your code before the DOM is loaded.

    Move it to the bottom of the page.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a function call from EF like: var result = context.myFunction(); the
Hi I have a Linq CodeBehind Function Like this var result = from m
I have a function like this <script type=text/javascript> function result() { return string; }
I have a label function like : private function formatDate (item:Object, column:DataGridColumn):String { var
So I have something like this: var xmlStatement:String = xmlObject.node[3].@thisValue; What mystery function do
Assume I have a function template like this: template<class T> inline void doStuff(T* arr)
I have a function that looks like this class NSNode { function insertAfter(NSNode $node)
I have a function like this in actionscript3 private function uploadFile(event:MouseEvent):void { var uploader:URLRequest
I have function like this in my header function bingframe() { var iframe =
I have a function like this: function ban_rot(){ var bnr = new Array(); bnr[0]

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.