Legend does not refresh

Hi all, I have two feature layers, which I toggle to display either State or sub-State information. My legend div is working fine but it shows the legend for the first feature layer selected at the start of the application. Switching to the sub-State layer refreshes the map but not the legend. Not sure what is incorrect here. Here is my code: 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"; } }

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 registered

The 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]);}

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 registered

The 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]);}