title: Uncaught TypeError: Cannot read property 'add' of undefined :: javascript :: bing maps
body: <p>so i am having problems with this displayinfobox function. I was looking into the Bing Maps v8 API trying to link up events with infobox. I am following Bing and storing event handlers in the infobox object. Im getting this read property 'add' error.</p>
  
  <p>In in the stacktrace, it is taking me to either the line where i am adding an event handler, or when im checking if infobox has any events attached to it.</p>
  
  <p>I am trying to follow this script here <a href="https://blueric.wordpress.com/2011/03/10/creating-hover-style-info-boxes-on-the-bing-maps-ajax-v7-0-control/" rel="nofollow">https://blueric.wordpress.com/2011/03/10/creating-hover-style-info-boxes-on-the-bing-maps-ajax-v7-0-control/</a>. Given it is quite old, i would assume things might work in similar ways.</p>
  
  <p><strong>Edited:</strong></p>
  
  <p>It seems like it has something to do with addHandler which im confused on.</p>
  
  <pre><code> function displayInfoBox(e) {
  
      stopInfoBoxTimer(e);
  
      console.log("after Infobox Timer");
  
      if (_displayInfoType != "INFOBOX")
          return;
  
      var replaceContent = infoboxTemplate;
      var ticket = e.target.ticket;
      var contentHead = "&lt;ul&gt;";
      var contentEnd = "&lt;/ul&gt;";
      var content = "";
      content += "&lt;li&gt;&lt;b&gt;:&lt;/b&gt; " + ticket.{REMOVED VAR} + "&lt;/li&gt;";
      content += "&lt;li&gt;&lt;b&gt;:&lt;/b&gt; " + ticket.{REMOVED VAR} + "&lt;/li&gt;";
      content += "&lt;li&gt;&lt;b&gt;:&lt;/b&gt; " + ticket.{REMOVED VAR} + "&lt;/li&gt;";
      content += "&lt;li&gt;&lt;b&gt;:&lt;/b&gt; " + ticket.{REMOVED VAR} + "&lt;/li&gt;";
      content += "&lt;li&gt;&lt;b&gt;:&lt;/b&gt; " + ticket.{REMOVED VAR}+ "&lt;/li&gt;";
      content += "&lt;li&gt;&lt;b&gt;:&lt;/b&gt; " + ticket.{REMOVED VAR}+ "&lt;/li&gt;";
      content += "&lt;li&gt;&lt;b&gt;:&lt;/b&gt; " + ticket.{REMOVED VAR}+ "&lt;/li&gt;";
  
      if (ticket.WW_PK != "")
          content += "&lt;li&gt;&lt;b&gt;:&lt;/b&gt; " + ticket.{REMOVED VAR}+ ", " + ticket.{REMOVED VAR}+ "&lt;/li&gt;";
      else
          content += "&lt;li&gt;&lt;b&gt;: &lt;span  style='color:red;'&gt;{REMOVED VAR}&lt;/span&gt;&lt;/b&gt;&lt;/li&gt;";
  
  
      replaceContent = replaceContent.replace('{title}', ticket.{REMOVED VAR});
      replaceContent = replaceContent.replace('{content}', contentHead + content + contentEnd);
  
      console.log("after replaced content");
      var options = {
          offset: new Microsoft.Maps.Point(23, 173),
          location: e.target.getLocation(),
          htmlContent: replaceContent,
          visible: true
      };
  
      console.log(options);
  
      console.log("before removing events from infobox");
      if (infobox != null &amp;&amp; infobox.mouseLeaveHandler != null &amp;&amp; infobox.mouseEnterHandler != null) {
  
          if (Microsoft.Maps.Events.hasHandler(infobox, 'mouseleave'))
              Microsoft.Maps.Events.removeHandler(infobox.mouseLeaveHandler);
  
          if (Microsoft.Maps.Events.hasHandler(infobox, 'mouseenter'))
              Microsoft.Maps.Events.removeHandler(infobox.mouseEnterHandler);
  
          infobox = null;
      }
      console.log("after removing events from infobox");
      console.log(infobox);
      infobox = new Microsoft.Maps.Infobox(location, options);
  //-------------------------- ERROR IN HERE ---------------------------------
  
      infobox.mouseLeaveHandler = Microsoft.Maps.Events.addHandler(infobox, 'mouseleave', infoBoxMouseLeave);
      infobox.mouseEnterHandler = Microsoft.Maps.Events.addHandler(infobox, 'mouseenter', infoBoxMouseEnter);
  //-------------------------- ERROR IN HERE ---------------------------------
      console.log(infobox.mouseEnterHandler);
      infobox.setMap(map);
   }
  </code></pre>
  
  <p><strong>Edited (LATEST):</strong> it seems like there is a bug with Bing V8 or just a change in the way this code is processed. I have copied most of the code into a fiddle project and it works fine in v7. So i am going to do this manually. </p>
  
  <p>I will post working code once, this is completed.</p>