Select to view content in your preferred language

Is it possible to put a legend widget in a container outside the viewDiv container?

341
2
Jump to solution
03-25-2025 04:10 PM
janzagar_0
Emerging Contributor

I want to place a legend div that I'm creating using the Legend widget in a HTML div that is not a child of the "viewDiv" element. Is this even possible? I really don't want to have a legend item floating around in my map div.

I have included a sample CodePen below for reference. I want the legend to go in the "sidebar" element.

https://codepen.io/janzagar0/pen/dPyjeYR

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JoelBennett
MVP Regular Contributor

Yes, you can create a legend as a child of any pre-existing element.  The following example shows getting a reference to an existing element via document.getElementById:

const legend = new Legend({
  view: view,
  container: document.getElementById("sidebar")
});

 

In your example, the "sidebar" element needs to be created and added to the document before creating the Legend though.

 

 

View solution in original post

2 Replies
JoelBennett
MVP Regular Contributor

Yes, you can create a legend as a child of any pre-existing element.  The following example shows getting a reference to an existing element via document.getElementById:

const legend = new Legend({
  view: view,
  container: document.getElementById("sidebar")
});

 

In your example, the "sidebar" element needs to be created and added to the document before creating the Legend though.

 

 

janzagar_0
Emerging Contributor

Thank you Joel. I realized I was separately making a really basic CSS-related error and the issue has now been fixed.

0 Kudos