Select to view content in your preferred language

Efficient Code for Turning Layers On/Off?

2993
2
Jump to solution
07-16-2015 03:12 PM
LauraMiles1
Frequent Contributor

Hi All,

I'm using the following from the ESRI JS samples to toggle my layers on and off, and it works well enough: Toggle layer visibility | ArcGIS API for JavaScript

However, getting each checkbox by its id seems a rather bloated way of doing things when you're working with ~100 layers. I'm pretty new to javascript so I'm not sure of the syntax to get the checkboxes by class, or if this can even work?

I've tried multiple id's:

on(dom.byId("chkVFPABoundary", "chkMunicipalBoundaries"), "change", updateCadastralLayerVisibility);

Which didn't work. Also:

on(document.getElementsByClassName(".cadastral_list_item"), "change", updateCadastralLayerVisibility); //Anything in my cadastral service has this class name

Which didn't work. I'm pretty sure I can't use 'on' with document.getElementsByClassName? Is there something comparable?

I tried jquery:

$('.cadastral_list_item').change(function() {
    if(this.checked) {
    updateCadastralLayerVisibility()
    }
});

This didn't work either, nothing happens at all when the checkbox is clicked.

I've been scouring the internet all day, but can't find any clues. Thanks in advance.

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
ReneRubalcava
Honored Contributor

How about

on(document.body, ".cadastral_list_item:change", updateCadastralLayerVisibility);

Any better?

View solution in original post

2 Replies
ReneRubalcava
Honored Contributor

How about

on(document.body, ".cadastral_list_item:change", updateCadastralLayerVisibility);

Any better?

LauraMiles1
Frequent Contributor

Works perfectly - thank you Rene.

0 Kudos