I have a web application built with v3.5 of the JS API. I have some Jquery functionality that changes the map being built based on user input. My problem is that I can't get the legend to stay in sync with the map. On the first selection I build a map with new esri.Map and I create a new esri.dijit.Legend on the map's onLayersAddResult event. With the first selection everything works and is pretty snappy. When a user chooses the next map setup I've tried two different techniques:

  1. The first is to build a brand new map. I call map.destroy() on the old one to get rid of it. I create a new map and add my layers to it but a new legend does not get created on the onLayersAddResult. The event fires but it seems it cannot create a new legend. If i use legend.destroy() before creating a new one, it blows away the whole div and can't create the legend either.
  2. The second technique is not to create a new map. I remove all the layers from the map and add the new layers to the map. Then I point legend.map to the map and use legend.refresh() with the new layers as an argument. This works, sort of. It seems legend.refresh is really slow and if my second map has more than 3 or 4 layers in it (which it does) it will never draw. In fact it hijacks the whole app and everything is frozen.

So can anyone give me some tips on how to use the legend dijit correctly? Either get rid of it properly so I can build a new one or have it refresh correctly and promptly with new map layers?

  • Can you post a jsfiddle with the issue in it?
    – CLJ
    Commented Jun 24, 2013 at 19:39
  • What version of ArcGIS Server are you using? The documentation says that is the layers are 10 or older then ArcGIS.com needs access to your layers to create the legend. Is that a possible issue? developers.arcgis.com/en/javascript/jsapi/legend.html
    – Jamie
    Commented Jun 24, 2013 at 19:44
  • I'm using ArcGIS Server 10.1. I'll see if I can put up some of the functionality in jsfiddle.
    – NBabel
    Commented Jun 24, 2013 at 20:03
  • Barebones jsfiddle - jsfiddle.net/JUkwV This just contains the javascript init functionality. When a user chooses a map we set the right case value and fire off dojo.ready(init)
    – NBabel
    Commented Jun 24, 2013 at 20:15
  • A couple thoughts: 1) In technique #2, have you tried calling legend.startup() again (instead of legend.refresh())? 2) Do you need to specify layerInfos to the legend constructor and refresh? If you just want to display all legends from all map layers, you can leave out the layerInfos argument altogether. 3) If you are concerned about destroying the "legendDiv", try creating another nested div (e.g., "legendDivInner") using dojo's dom-construct that can be safely destroyed when legend.destroy() is called. Then you can recreate the div as needed.
    – greenlaw
    Commented Jun 24, 2013 at 20:34

1 Answer 1


According to the documentation, you should not need to call refresh() on the legend dijit, unless you specify the layerinfos when constructing the legend.

Calling refresh is only necessary when layerInfos is used in the Legend constructor, otherwise the legend will refresh the layers automatically.


  • Thanks, greenlaw answered the question above. legend.destroy is what I need to use, just didn't know how to recreate the div.
    – NBabel
    Commented Jun 26, 2013 at 16:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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