how to programmatically uncheck checkboxes?

20699
16
Jump to solution
02-17-2016 07:58 AM
DanielSchatt
New Contributor III

hi, I'm new to the Javascript API and just trying to programmatically uncheck checkboxes.  An example of my code (using a checkbox with id "slrbxC") is::

document.getElementbyId("slrbxC").checked = false;

But the checkbox is not unchecking.  Is there some other code I need to add to make this happen?

Thanks!

Dan

0 Kudos
16 Replies
DanielSchatt
New Contributor III

thanks Ken!  Appreciate your time.  Well I guess this is another question:  I'd like to post my code but I'm new to GeoNet and I don't see how to post blocks of code that are easy to read.  The old ESRI forums had a "code" button that wrapped your code up and made it easy to post and read there but I don't see anything like that here.  Am I missing something?  Sorry, thanks again.

0 Kudos
TyroneBiggums
Occasional Contributor III

Click reply.

Click "use advanced editor" on the upper right of your little input window.

Click the >>.

Select a javascript.

KenBuja
MVP Esteemed Contributor

The other way you can do this is to put it on a site like JSBin or JSFiddle.

0 Kudos
TyroneBiggums
Occasional Contributor III

I didn't realize you were using dojo's checkbox dijit. There are a couple of things you could try.

First, check to see if your line of code in your original post is inside of a dojo/domReady. Is it possible that you are referencing an object that does not exist yet? Keep in mind that some things (not necessarily this one) occur asynchronously. If you reference a dom element by id that hasn't rendered completely, there is no dom element to reference.

You could add an onChange event to your checkbox. This documentation is pretty terrible but, https://dojotoolkit.org/reference-guide/1.10/dijit/form/CheckBox.html#

I would sooner use straight up javascript (or jQuery) create checkbox before using a dojo dijit for a dom element like that... especially after I played with the example on dojotoolkit.org for a little bit.

JeffJacobson
Occasional Contributor III

I also did not realize you were referring to the Dojo checkbox dijit rather than a regular HTML checkbox. The sample below shows how each type of checkbox can be programmatically checked.

I agree with @Tyrone Biggums in that I would just use a regular HTML checkbox rather than the Dojo checkbox, as the Dojo checkbox just adds an extra layer of complexity.

<input type="checkbox" id="dojoCheckbox" checked="checked" />
<input type="checkbox" id="normalCheckbox" checked="checked" />
<button type="button" id="theButton">
  Toggle the checkboxes
</button>

require(["dijit/form/CheckBox", "dijit/registry"], function(CheckBox, registry) {
  // Create the Dojo checkbox
  var cb = new CheckBox({
    checked: true
  }, "dojoCheckbox");
  // Get a reference to the button.
  var b = document.getElementById("theButton");

  // set the button click event
  b.onclick = function() {
    // Use the registry to get the checkbox dijit.
    cb = registry.byId("dojoCheckbox");
    // Set the dojo checkboxes "checked" property to be 
    // opposite what it currently is.
    cb.set("checked", !cb.checked);
    // Get the regular checkbox and check it.
    var ncb = document.getElementById("normalCheckbox");
    ncb.checked = !ncb.checked;
  };
});
Kathleen_Crombez
Occasional Contributor III

The way you are declaring the checkbox, it looks like it is a dojo checkbox and would more than likely need to be access with Ken Buja solution.

dijit.byId("slrbxC").set("checked", false);

0 Kudos
DanielSchatt
New Contributor III

Thanks much to Jeff, Tyrone, Ken, and everyone else for the help!  After much trial and error, this is the syntax that worked:

var slrBoxC = new Checkbox({

  id: "slrbxC",

  name: "slrbxC"

});

slrBoxC = registry.byId("slrbxC");

slrBoxC.set("checked", false);

0 Kudos