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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:49:26+00:00 2026-05-16T23:49:26+00:00

I have this code inside the success function of a jQuery ajax call success:

  • 0

I have this code inside the success function of a jQuery ajax call success: function(d)

for (var n in d.items)
{        
    google.maps.event.addListener(markers[d.items[n].id], 'mouseover', function() {
        focusMarker(d.items[n].id);
    });
}

Unfortunately, the function always evaluated d.items[n].id as the last item in d.items collection.

I tried making this modification:

for (var n in d.items)
{        
    var id = d.items[n].id;        
    google.maps.event.addListener(markers[d.items[n].id], 'mouseover', function() {
        focusMarker(id);
    });
}

but my function always returned the same thing.

Is this a scope problem, or is there something wrong with my function definition?

  • 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-16T23:49:26+00:00Added an answer on May 16, 2026 at 11:49 pm

    There are several ways to solve this problem, the most common is to use a function to preserve the looping values:

    for (var n in d.items) {
      (function(id) {
        google.maps.event.addListener(markers[id], 'mouseover', function() {
            focusMarker(id);
        });
      })(d.items[n].id);
    }
    

    By the way, if d.items is an array, I would recommend you to use a sequential loop e.g.:

    for (var n = 0; n < d.items.length; n++) {
      //..
    }
    

    The for-in statement is meant to be used to enumerate over object properties.

    If you use it on arrays or array like objects, it can give you several problems, first, inherited properties are also enumerated, meaning that if someone augments the Array.prototype object, those properties will be also enumerated in your loop.

    Also the order of iteration is not guaranteed by the specification, the properties may not be visited in the numeric order.

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

Sidebar

Related Questions

I have this block of code inside a loop: var points = [new google.maps.LatLng(lat1,
I have this code inside a iframe: window.addEventListener('message', function(e){ if(e.data == 'test') console.log(e); },
I have this code inside thread and when I call terminate it terminates fast
I have this code snippet inside a function that checks if an object exists
I have function: function get_playlist(){ var result = jQuery.ajax({ url: '<?php echo admin_url('admin-ajax.php'); ?>',
I have this function that makes an ajax call. I'm describing the problem in
I have a following (simplified) code: var myModule = { submitDummyForm: function(){ console.log(this); //
I have this code inside a header (edited): template <int i> class A {};
So I have this code inside my lib/ folder: class GlobalConfig::SetHelper def self.yes_no_input(configuration) value
I have written this code inside a servlet to delete certain records from three

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.