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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:06:25+00:00 2026-06-16T19:06:25+00:00

Possible Duplicate: Javascript closure inside loops – simple practical example I’m having a problem

  • 0

Possible Duplicate:
Javascript closure inside loops – simple practical example

I’m having a problem with a closure in a phonegap application. I have a JSON object which contains instructions on how to create a form. This is roughly the code:

for (var i in this.form.elements) {
    var element = this.form.elements[i];
    switch (element.type) {
        // other cases
        case 5:
            var addThumb, photoInput;
            addThumb = function (domId) {
                return function (imgData) {
                    console.log('id: ' + domId);
                    $(domId).attr({src: 'data:image/jpeg;base64,' + imgData});
                }
            }('#thumb-'+element.id);
            photoInput = $('<input>')
                .attr({type: 'button', value: element.name, name: element.type, id: element.id, 'class': 'photobutton'})
                .click(function (filename, cb) {
                    return function() {
                        console.log('taking photo and saving to ' + filename)
                        takePhoto(filename,addThumb)
                    }
                }('photo-'+element.id, addThumb)
            );
            photoThumb = $('<img>').attr({id: 'thumb-'+element.id, 'class': 'thumbnail', src: 'img/target.png'});
            $('#content').append(photoInput, photoThumb);
            break;
    }
}

Clicking the photobutton opens up a camera to take a picture, which is saved to disk. Once saved, addThumb is called with the base64 photo data to replace the thumbnail image. The output for each for my test form is:

taking photo and saving to file photo-686
id: '#thumb-690

The closure for taking the photo works, but when addThumb is called as a callback, domId shows the last element.id that went through the for loop. Given two thumbnails, the second one is always replaced regardless of the button pressed. It happens on both iPhone and Android so there must be a problem with the way I’ve written it. What did I do wrong?

  • 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-16T19:06:26+00:00Added an answer on June 16, 2026 at 7:06 pm
    takePhoto(filename, addThumb)
    

    You need to pass the cb variable from inside the closure here, not the addThumb which is not “protected”. Seems like you forgot to change the variable name when “protecting” it.

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

Sidebar

Related Questions

Possible Duplicate: Javascript closure inside loops - simple practical example Javascript infamous Loop problem?
Possible Duplicate: Javascript closure inside loops - simple practical example Please explain the use
Possible Duplicate: Javascript closure inside loops - simple practical example javascript variable scope/closure in
Possible Duplicate: Javascript closure inside loops - simple practical example I have an array
Possible Duplicate: Javascript closure inside loops - simple practical example I was trying to
Possible Duplicate: Javascript closure inside loops - simple practical example I add event handlers
Possible Duplicate: Javascript closure inside loops - simple practical example I'm creating a table
Possible Duplicate: Javascript closure inside loops - simple practical example Calling setTimeout function within
Possible Duplicate: Javascript closure inside loops - simple practical example I am using webservice
Possible Duplicate: Javascript closure inside loops - simple practical example How can I make

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.