Getting the following error when running my app:

Error: Tried to register widget with id==legendDiv but that id is already registered

Not sure how to get rid of it. My javascript for the legend is:

//Add the Legend
          layer: layer,
          title: ''
      dojo.connect(map, 'onLayersAddResult', function (results) {
          var legend = new esri.dijit.Legend({
              map: map,
              layerInfos: legendLayers
          }, "legendDiv");

      if (layer.loaded) {
      } else {
          dojo.connect(layer, "onLoad", buildLayerList);

and my HTML is:

<div dojoType="dijit.TitlePane" title="Legend" closable="false"  open="false" style="float:right;">
                <div dojoType="dijit.layout.ContentPane" style="width:180px; overflow:auto;">
                <span id="legendDiv"></span>

Can anybody suggest a workaround or a fix to the error. I've done some searching and it looks like I might need to do a destroy? Not sure how to do that.


Add the legend on map load, the onLayersAddResult is firing multiple times which means it is trying to create more than one legend of the same name. Connect to the onLoad event

dojo.connect(map, 'onLoad', function (results) {

  • This eliminated the error that I described, so I'll mark it as the answer, however, it then triggers 2 "TypeError: Unable to get value of the property 'then': object is null or undefined" errors. Thanks for your help. – Craig Dec 3 '12 at 20:03
  • Apologies, yes you can use the onLayersAddResult but the cause of the error is trying to load the legend twice. There is some of your code missing, I'm not sure what buildLayerList is but I suspect that this is also calling the legend to startup. I've put together a fiddle here showing a legend using onLayersAddResult jsfiddle.net/dave_wilton/cYXph – David Wilton Dec 4 '12 at 8:29
  • Turns out I was using map.addLayers() twice... so it was firing the legend twice. Thanks for the fiddle. – Craig Dec 4 '12 at 13:08

var legend = new esri.dijit.Legend({ map: map, layerInfos: legendLayers }, "legendDiv");

works two times. You must call it only one time. Whatever you can fix the "id is already register" error with dijit's destroy function. For this example

if(legend){ legend.destroy(); }

legend = new esri.dijit.Legend({ map: map, layerInfos: legendLayers }, "legendDiv");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.