I get the id of a li element and want to use the name to access that part of an object. I have tried to add the var selectionId which contains the name of the section I want, such as cars but i get an error. Is there a way?
Thanks.
$(document).ready(function() {
$(".markerSelection").click(function() {
var selectionId = $(this).attr("id");
drop(selectionId);
});
});
var markers = {
shopping : [
new google.maps.LatLng(52.26183, -7.11339),
new google.maps.LatLng(52.26134, -7.11226),
new google.maps.LatLng(52.26067, -7.11181),
new google.maps.LatLng(52.26003, -7.11033)],
cars : [
new google.maps.LatLng(52.26183, -7.11339),
new google.maps.LatLng(52.26134, -7.11226),
new google.maps.LatLng(52.26067, -7.11181),
new google.maps.LatLng(52.26003, -7.11033)]
};
var iterator = 0;
function drop(selectionId) {
clearOverlays();
for (var i = 0; i < markers.selectionId.length; i++) {
setTimeout(function() {
addMarker();
}, i * 200);
}
}
Tony
10.4k3 gold badges51 silver badges78 bronze badges
asked Jul 14, 2012 at 22:07
Keith Power
14.2k24 gold badges71 silver badges140 bronze badges
1 Answer 1
If you are trying to access an object property by a variable key, you need to use the array notation:
... markers[selectionId].length ...
markers.selectionId fails because it is equivalent to markers["selectionId"].
answered Jul 14, 2012 at 22:10
Hamish
23.4k8 gold badges57 silver badges68 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Elliot Bonneville
What downvote? -innocent halo- [may or may not have removed downvote due to misinterpreting answer]
lang-js