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.
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; }; });
Hey Dan,
Have you tried:
document.getElementById("slrbxC").checked = false;
Note, "By" in "getElementById" should be capitalized.
Ryan
thanks Ryan, my typo in my post here but yes I did have it capitalized in my code...
What you are doing (aside from the aforementioned typo) is correct...so it may be a problem with the ID value you're passing. If you're using ASP.NET or something similar, an ID you assign can be transformed with an added prefix.
In place of the line you have, try this below. It'll let know what the actual ID is if this is the case. If you get the "element not found" alert, then it couldn't find the checkbox at all, which is a different problem altogether...
var inputs = document.getElementsByTagName("INPUT"); var found = false; var input; for (var x = 0; x < inputs.length; x++) { input = inputs; if ((input.type) && (input.type.toLowerCase() == "check") && (input.id.indexOf("slrbxC") >= 0)) { input.checked = false; alert("Set...id = [" + input.id + "]"); found = true; break; } } if (!found) alert("element not found...");
You generally have to change a property with the "set" method
document.getElementById("slrbxC").set("checked", false);
Also, you should use the syntax
dijit.byId("slrbxC")
to get the reference of the checkbox.
Here's a working sample: JS Bin - Collaborative JavaScript Debugging
Aside from your typo, your code should work as long as the ID property is correct.
// Either of these should work document.getElementById("myCheckBox").checked = false; document.getElementById("myCheckBox").removeAttribute("checked");
To make sure you have the ID correct, this code snippet will list all of the checked items and log their ids to the console. (If you're using IE 11 or below, you'll need to use a for loop instead of Array.from.)
var checkedItems = document.querySelectorAll(":checked"); Array.from(checkedItems, function(cb){ console.debug(cb.id); });
You could try to use dev tools (F12) and make sure that your ID is correct once your stuff is running.
I appreciate the tips from everyone! Based on the code given above, apparently it's not finding the id. I haven't learned to use the debugging tools yet but before digging into that, here's my check box declaration:
var slrBoxC = new CheckBox({
id: "slrbxC"
name: "slrbxC"
});
did I do this wrong somehow? shouldn't the id value be "slrbxC" or am I missing something? Thanks again..
If using the code from my example doesn't work, can you post an example of your code that shows the issue?