How do I get my layer checkboxes to update layer visibility?

6210
22
Jump to solution
05-01-2015 11:37 AM
ChrisSergent
Regular Contributor III

I am trying to use this sample to toggle layers: Toggle layer visibility | ArcGIS API for JavaScript  but I receive this error message:

TypeError: Cannot read property 'on' of null {stack: (...), message: "Cannot read property 'on' of null"}

  1. message: "Cannot read property 'on' of null"
  2. stack: (...)
  3. get stack: function () { [native code] }
  4. set stack: function () { [native code] }
  5. __proto__: Error

"in domReady callback"

"TypeError: Cannot read property 'on' of null

and here is my code:

var visibleLayerIds = [];
    on(dom.byId("lyrSigns"), "change", updateLayerVisibility);
    on(dom.byId("lyrSupports"), "change", updateLayerVisibility);


    function updateLayerVisibility() {
        var inputs = query(".list_item");
        var inputCount = inputs.length;
               


        for (var i = 0; i < inputCount; i++) {
            if (inputs.checked) {
                visibleLayerIds.push(inputs.value);
            }
        }


        if (visibleLayerIds.length === 0) {
            visibleLayerIds.push(-1);
        }


        layer.setVisibleLayers(visibleLayerIds);
    }

And here is my project on github: csergent45/streetSigns · GitHub

Tags (2)
22 Replies
StevenGraf1
Occasional Contributor III

Ignore the <span> </span>....It put that in there because I copied my original post.

ChrisSergent
Regular Contributor III

I modified the code as follows:

var checkBox = new CheckBox({
        name: "lyrSigns",
        value: "agreed",
        checked: false,
        onChange: function (b) {
            if (b == true) {
                app.map.addLayer("signLayerUrl");
            } else {
                app.map.removeLayer("signLayerUrl");
            }
        }
    }, "lyrSigns").startup();

And I get the following error: Uncaught lang.hitch: scope["onLoad"] is null (scope="[object Window]")

0 Kudos
StevenGraf1
Occasional Contributor III

Add in the following module dojo/_base/lang — The Dojo Toolkit - Reference Guide  and see if that corrects the error.