0

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

1 Answer 1

4

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
Sign up to request clarification or add additional context in comments.

1 Comment

What downvote? -innocent halo- [may or may not have removed downvote due to misinterpreting answer]

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.