I know this seems like a common error and I have tried to implement some jquery or Dojo code to fix the problem but nothing has helped so far--I keep getting the same error: Widget Already Registered with ID...
Here is my code:
$( "div" ).remove( "#div_temperature_legend" ); //jquery
dojo.create("div", { id: "div_temperature_legend", innerHTML: "<p></p>" }, "legendPanel");
var legend = new esri.dijit.Legend({
map: map,
layerInfos: [{ layer: layer, title: "Temperature" }],
}, "div_temperature_legend");
legend.startup();
the above code is in a button click function and that function is itself part of a large dojo require() function; and works the first time. The jQuery line does seem to remove because the legends don't get duplicated in the newly created div_temperature_legend layer.
Anyway, I have tried dojo's destroy() function but I still get the same error. I guess somehow, somewhere I need to register this div and then destroy it. But how? where? In the context of my code?
Ideally, code should detect the widget if it exists and doesn't create another legend.
Thanks.
div, but the reference of the widget (i.e esri.dijit.Legent is still available in the registry. To destroy the widget, which removes the reference in the registry you need to do something like this.legend.destroy()before creating anew esri.dijit.Legend. - frank