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);
    }
}

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"].

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]

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.