I'm only just getting into dojo, so I have what most would probably consider a very basic question. I've built my own legend in a div and added divs for the legend items. This is all quite manual, but I just can't get my head around the dojo dijits. I've written the code to toggle the divs on and off (just changing their background colour), but what I now need to add is the code to show or hide the layers from the map.

How can I use the ArcGISDynamicMapServiceLayer object I created to show and hide that entire service on the map after it's been constructed? All the examples I've seen look at the service and then list the layers inside it. I just want to add/remove or show/hide the entire service.


You can set visible to false if you don't want it to be on when the map is loaded. An example using ArcGISDynamicMapServiceLayer is below:

    var layer1 = new esri.layers.ArcGISDynamicMapServiceLayer("path/to/your/service", {
        id: 'layer1',
        opacity: 1,
        visible: false
    var layer2 = new esri.layers.ArcGISDynamicMapServiceLayer("path/to/your/service", {
        id: 'layer2',
        opacity: 1,
        visible: true

map.addLayers([layer1, layer2]);

Added to respond to comment

If you are looking for a nice, easy to use legend/table of contents that allows for the toggling of service visibility, I would suggest theTable of Contents (TOC)/Legend Widget for JavaScript API It is what I use in all my apps.

Additionally, ESRI has a sample that shows how to toggle services as well. As well as a sample that shows how to dynamically create the layer list that can be toggled on and off.

  • Thanks for the response @Craig. I added a bit of clarification in the question. I'm specifically trying to change the visibility after construction of the layers. So, I'd load the layers, make them visible or not and then add functionality to a legend where I can change their visibility during runtime. What would be the command to change the visbility? – MierMoto Sep 4 '13 at 23:19
  • Please see my edited answer above. – Craig Sep 5 '13 at 12:04
  • Sorry @Craig, but that's specifically what I tried to avoid. Not for any other reason that the fact that I can't figure out how to incorporate the widgets with my own stylesheets. I know, my skills are pretty basic at the moment, but I'll probably get to a point where I understand Dojo better. For the time being, I've created my own div and I'll manually load and toggle the layers. Thanks again, but although your answer is technically correct, it wasn't what I was looking for this time. – MierMoto Sep 5 '13 at 22:23

I've managed to find the solution after countless iterations of trying different lines of code I thought might work. After constructing the layer I can use the following to switch it on:


To switch it off, I can user:


See, simple. I just didn't know the exact syntax for both and i kept guessing that false would pass "false" as the argument.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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