Hi all,I found the solution to this issue. My JavScript console gave the following warning:Tried to register widget with id==legend but that id is already registeredThe solution is to destroy any previously registered widget and recreate it. I added this code (marked in red) right before where I define the parameters of the legendDijit widget:function viewIndicator( {....if (unitStActiveM==true){stateFeatureLayer.selectFeatures(query, esri.layers.FeatureLayer.SELECTION_NEW);mapM.removeLayer(distFeatureLayer);dojo.connect(stateFeatureLayer, "onSelectionComplete", mapM.addLayers(stateFeatureLayer));showLegend();mapM.addLayers([stateFeatureLayer]);}else if (unitDistActiveM==true){distFeatureLayer.selectFeatures(query, esri.layers.FeatureLayer.SELECTION_NEW);mapM.removeLayer(stateFeatureLayer);dojo.connect(distFeatureLayer, "onSelectionComplete", mapM.addLayer(distFeatureLayer));showLegend();mapM.addLayers([distFeatureLayer]);}if (dijit.byId("legendDivHolder").domNode.style.visibility = "hidden"){dijit.byId("legendDivHolder").domNode.style.visibility = "visible";}else {dijit.byId("legendDivHolder").domNode.style.visibility = "visible";}}function showLegend() {dojo.connect(mapM,"onLayersAddResult",function(results){var layerInfo = dojo.map(results, function(layer,index){if (unitStActiveM==true){return {layer:stateFeatureLayer, title: "stateFeatureLayer"};}else if (unitDistActiveM==true){return {layer:stateFeatureLayer, title: "distFeatureLayer"};}});if(layerInfo.length > 0){if(legendDijit){ legendDijit.destroyRecursive(true); }legendDijit = new esri.dijit.Legend({map:mapM,layerInfos:layerInfo},"legend");legendDijit.startup();legendDijit.refresh();}});}ThanksSamir