title: Google Maps JavaScript API v3 dynamic infowindow
body: <p>I am adding markers dynamically like this: </p>
  
  <pre><code>var oldMarkers = null;
      return function(map, locations){
          //Clearing markers, if they exist
          if(oldMarkers &amp;&amp; oldMarkers.length !== 0){
              for(var i = 0; i &lt; oldMarkers.length; ++i){
                  oldMarkers[i].setMap(null);
              }
          }
          //Adding new markers
          oldMarkers = [];
          for (var i = 0; i &lt; locations.length; i++) {
              var newmarker = locations[i];
              var myLatLng = new google.maps.LatLng(newmarker[1], newmarker[2]);
              var marker = new google.maps.Marker({
                 position: myLatLng,
                 map: map,
                 shape: shape,
                 title: newmarker[0],
                 zIndex: newmarker[3],
                 clickable: true
              });
  oldMarkers.push( marker );
   }
  </code></pre>
  
  <p>Works well based on a jQuery "click"  function.  What I would like to do now is add an  infowindow to each marker.  OK I have this code from Google which should/does go before <code>oldMarkers.push( marker )</code>:</p>
  
  <pre><code>var j = i + 1;
  marker.setTitle(j.toString());
  attachSecretMessage(marker, j);
  </code></pre>
  
  <p>and this function:</p>
  
  <pre><code>function attachSecretMessage(marker, number) {
    var message = ["This","is","the","secret","message"];
    var infowindow = new google.maps.InfoWindow(
        { content: message[number],
          size: new google.maps.Size(50,50)
        });
    google.maps.event.addListener(marker, 'click', function() {
      infowindow.open(map,marker);
    });
  }
  </code></pre>
  
  <p>It sort of works, in that an info box appears for 2 markers where it should do, but not for all.  This is what/where I don't understand:  If I have an array to make new markers like this (only an example):</p>
  
  <pre><code>var essentials = [
  ['Marker 1', 12.254181, 12.649962 , 9],
  ['Marker 2', 12.254181, 12.649962 , 9],
  ['Marker 3', 12.254181, 12.649962 , 9],
  ['Marker 4', 12.254181, 12.649962 , 9],
  }
  </code></pre>
  
  <p>What I need/would like to do is add an array key for the content of the infowindow so a bit like this:</p>
  
  <pre><code> var essentials = [
      ['Marker 1', 12.254181, 12.649962 , 9, 'Info Window content']
  </code></pre>
  
  <p>How do I pass <code>newmarker[4]</code> to the infowindow?</p>