Your first 3 minutes are FREE talking live with me.
Please visit my website at: http://www.keen.com/Ask+Fran
Or, call me right now at: 1-800-275-5336 x0160
--
You received this message because you are subscribed to the Google Groups "Website Design Nz" group.
To unsubscribe from this group and stop receiving emails from it, send an email to website-design-nz+unsubscribe@googlegroups.com.
To post to this group, send email to website-design-nz@googlegroups.com.
Visit this group at http://groups.google.com/group/website-design-nz.
For more options, visit https://groups.google.com/d/optout.
Re: Evaluating variable in loop at time of creation
What you need to do is create a closure.
On Wednesday, May 7, 2014 9:05:16 PM UTC-4, Anthony Glazebrook wrote:
-- for(i=0;i<locations.length;i++) {
//var thisLocation;
//thisLocation = eval('location' + (i+1));
coords = locations[i];
mapLocations[i] = L.mapbox.featureLayer({
type: 'Feature',
geometry: {
type: 'Point',
coordinates: coords
},
properties: {
'marker-size': 'large',
'marker-color': '#ec0112'
}
}).addTo(map);
mapLocations[i].on('click', function(i) {
// This is a closure. i becomes a local variable
// to the function below.
return function () {
$('.rental-location').css('display', 'none');
$('.rental-location-' + (i+1)).css('display', 'block');
};
}(i);
}
On Wednesday, May 7, 2014 9:05:16 PM UTC-4, Anthony Glazebrook wrote:
I am creating markers on a Mapbox page and when the marker is clicked, it needs to open a different layer on the calling page.I tried creating a loop where the marker click event was assigned automatically based on the index of the loop, but this evaluated the index value at the time of running, rather than the time of creation.How can I extract this index value and have it called at the time of running?Here is the code:// Create a map in the div #mapvar map = L.mapbox.map('map', 'marashi760.heap66lg').setView([lat, lng], zoom);var locations = [[-122.386839, 37.650615],[-118.398227, 33.94466],[-117.191262, 32.744102],[-117.860223, 33.680488],[-118.144392, 33.818421],[-122.219743, 37.712569],[-121.925906, 37.366695],[-121.590065, 38.695085],[-117.346057, 33.163207]];var mapLocations = new Array();for(i=0;i<locations.length;i++) { //var thisLocation;//thisLocation = eval('location' + (i+1));coords = locations[i];mapLocations[i] = L.mapbox.featureLayer({type: 'Feature',geometry: {type: 'Point',coordinates: coords},properties: {'marker-size': 'large','marker-color': '#ec0112'}}).addTo(map);}mapLocations[0].on('click', function() {$('.rental-location').css('display', 'none'); $('.rental-location-1').css('display', 'block'); });mapLocations[1].on('click', function() {$('.rental-location').css('display', 'none'); $('.rental-location-2').css('display', 'block'); });mapLocations[2].on('click', function() {$('.rental-location').css('display', 'none'); $('.rental-location-3').css('display', 'block'); });// etc for all markersI tried adding this into the loop:mapLocations[i].on('click', function() {$('.rental-location').css('display', 'none'); $('.rental-location-' + (i+1)).css('display', 'block');}but it the penultimate line always evaluates (i+1) to the value of i at the time of running, rather than creation. It seems silly that I have to manually assign the click functionality rather than iterate through the markers.
You received this message because you are subscribed to the Google Groups "JavaScript Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to javascript-information+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Evaluating variable in loop at time of creation
I am creating markers on a Mapbox page and when the marker is clicked, it needs to open a different layer on the calling page.
-- I tried creating a loop where the marker click event was assigned automatically based on the index of the loop, but this evaluated the index value at the time of running, rather than the time of creation.
How can I extract this index value and have it called at the time of running?
Here is the code:
// Create a map in the div #map
var map = L.mapbox.map('map', 'marashi760.heap66lg')
.setView([lat, lng], zoom);
var locations = [
[-122.386839, 37.650615],
[-118.398227, 33.94466],
[-117.191262, 32.744102],
[-117.860223, 33.680488],
[-118.144392, 33.818421],
[-122.219743, 37.712569],
[-121.925906, 37.366695],
[-121.590065, 38.695085],
[-117.346057, 33.163207]
];
var mapLocations = new Array();
for(i=0;i<locations.length;i++) {
//var thisLocation;
//thisLocation = eval('location' + (i+1));
coords = locations[i];
mapLocations[i] = L.mapbox.featureLayer({
type: 'Feature',
geometry: {
type: 'Point',
coordinates: coords
},
properties: {
'marker-size': 'large',
'marker-color': '#ec0112'
}
}).addTo(map);
}
mapLocations[0].on('click', function() {
$('.rental-location').css('display', 'none');
$('.rental-location-1').css('display', 'block');
});
mapLocations[1].on('click', function() {
$('.rental-location').css('display', 'none');
$('.rental-location-2').css('display', 'block');
});
mapLocations[2].on('click', function() {
$('.rental-location').css('display', 'none');
$('.rental-location-3').css('display', 'block');
});
// etc for all markers
I tried adding this into the loop:
mapLocations[i].on('click', function() {
$('.rental-location').css('display', 'none');
$('.rental-location-' + (i+1)).css('display', 'block');
}
but it the penultimate line always evaluates (i+1) to the value of i at the time of running, rather than creation. It seems silly that I have to manually assign the click functionality rather than iterate through the markers.
You received this message because you are subscribed to the Google Groups "JavaScript Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to javascript-information+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Subscribe to:
Posts (Atom)