Change the attribute visible of layer when click on checkbok?

Jump to solution
10-18-2019 12:04 PM
New Contributor II

i have this code to detect change on my checkbox.

if is true turn on layer if is not true turn off layer

in my map service the layer have id 14

$(document).ready(function() {

$('#checkboxOrtofoto10m').change(function() {
if(this.checked) {
Layers.findSublayerById(14).labelsVisible = true;

in console mark this 

any suggestions?
0 Kudos
1 Solution

Accepted Solutions
Esri Contributor
4 Replies
Esri Contributor

Here is a sample that toggles sublayer visibility: MapImageLayer - Toggle sublayer visibility | ArcGIS API for JavaScript 4.13 

Esri Regular Contributor

Hmm, I tested this functionality as a sanity check, and it is possible to do.

Test app (toggle state labels on and off with the button at the bottom) 

Perhaps the MapServer you're using doesn't support dynamic layers? You need it to in order to make changes dynamically.

When you inspect the MapServer from REST, do you see:

Supports Dynamic Layers: true


New Contributor II

Thank you for answering if it supports Dynamic Layers, my quick stop solution that works for me is to load the map again, for now I will stay with this solution later I will see the performance. 

New Contributor II

I have the solution i only need findSublayerById function .

$('#checkbox').change(function(event) {
var idcheck ="data-id");
var sublayer = Layers.findSublayerById(parseInt(idcheck));
sublayer.visible = !sublayer.visible;