0

I want to populate a drop-down list with data from an array. The code below is something I would do if I was working with angularJS. I want to know if there is a way to do this in javascript or somehow allow ng-options to work here.

  var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
  var mapOptions = {
    zoom: 4,
    center: myLatlng
  };

  var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

    var city = ["NewYork", "Chicago", "Florida"];

  var contentString = 
      `<div class="dropdown">
   <select>
     <option ng-options="item for item in ${city}">{{item}}</option>
   </select>
</div>`;

  var infowindow = new google.maps.InfoWindow({
      content: contentString
  });

  var marker = new google.maps.Marker({
      position: myLatlng,
      map: map,
      title: 'Uluru (Ayers Rock)',
//    zIndex: Math.round(myLatlng.lat() * -100000) << 5
  });
  google.maps.event.addListener(marker, 'click', function() {
    infowindow.setContent(contentString);
    infowindow.open(map,marker);
  });

DEMO: https://jsfiddle.net/spdmu6we/5/

5
  • Here is a JavaScript solution Commented Sep 30, 2020 at 20:24
  • @RandyCasburn this works but is there a way to implement this with the way I had it, without having to put the HTML in the HTML section and having the content string variable there. Commented Sep 30, 2020 at 20:32
  • Yes, you have two options: 1. Use string parsing to build up a string of HTML and then pass that as your content 2. Parse the string you have into a DOM Node and use the code I provided. Commented Sep 30, 2020 at 20:36
  • Here's the string concatenation solution: jsfiddle.net/1wrmde9y Commented Sep 30, 2020 at 20:43
  • Yeah this works, you can put this as the answer so I can accept it Commented Sep 30, 2020 at 20:57

2 Answers 2

1

Per your comment, you prefer to do this without and HTML `'.

So, you have two options: 1. Use string parsing to build up a string of HTML and then pass that as your content 2. Parse the string you have into a DOM Node and use the code I provided.

This replaces the GMap stuff with generic code. You will still be able to use the code related to the DOM.

Here is a solution:

var city = ["NewYork", "Chicago", "Florida"];
const pre = '<div class="dropdown"><select>';
const post = '</select></div>';
let options = '';
city.forEach(city => {
  let o = `<option value=${city}>${city}</option>`;
  options += o;
});
content = pre + options + post;

var infowindow = document.createElement('div');
infowindow.innerHTML = content;
infowindow.style.display = 'none'

var marker = document.createElement('button');
marker.innerText = "Click me";

marker.addEventListener('click', () => {
  document.body.appendChild(infowindow);
  infowindow.style.display='block';
});
document.body.appendChild(marker);
<div id="map-canvas"></div>

Sign up to request clarification or add additional context in comments.

Comments

0

The two typical ways of creating a lot of elements in javascript is either:

  1. concat a long string of the elements together, like "<option>Banana</option><option>Apple</option>" and then use .innerHTML on the parent select

  2. use javascript to create each node independantly like document.createElement("OPTION") and .appendChild it.

There's performance implications on each. But native javascript doesn't do that kind of templating.

2 Comments

Well I tried the second option and I keep getting a null error jsfiddle.net/wegs6okb/1
That's because you don't have a select element in the HTML, yet you are trying to access it.

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.