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
Solved! Go to Solution.
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.
Click reply.
Click "use advanced editor" on the upper right of your little input window.
Click the >>.
Select a javascript.
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.
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; }; });
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);
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);