0

I have some google map markers that sit on a map. They are controlled by the rout-markers id. When I do this command:

document.getElementById('rout_markers').style.display = 'none';

I am able to make all existing markers disappear from the map, which is what I want. But if new markers are added, I am not able to add them to the rout_markers element by doing this:

document.getElementById('rout_markers').value = str;

I mean, the markers do appear on the map, just not in the rout_markers id and I can't pass them to the request when I need to. Is there some way I can get the newly-created markers to get attached to the rout_markers id again?

Is this a generally accepted method of doing things in JS still?

Thanks, Alex

1
  • You'll need to post your HTML, I think, because this doesn't make much sense. One thing to note: "id" values really must be unique on a page - if you re-use an "id" for more than one element, your page is invalid. The "class" attribute is what you should use to give elements a "type" or "category" (or, I guess, a "class" :-) Commented Apr 10, 2011 at 20:10

1 Answer 1

1

you should collect all markers in an object with known keys then find the marker then do what you want to do.

I think, you should have

  • markers Object that holds all markers in the map
  • addMarkerToMap Function that adds new marker to map
  • getMarker Function that gets a marker

and the codes should be like this

var markers = {} 
var addMarkerToMap = function(id) {
    var marker = ... // create a new marker and set given id argument as marker id and append marker to map
    markers[id] = marker; // this will inject the created marker to markers Object
} 
var getMarker = function(id) {
    return markers[id];
} 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.