views:

309

answers:

4

Okay, so I'm trying to change a checkbox's state programmatically in dashcode. I've tried:

var checkbox = document.getElementById("checkbox");

// I have tried all the following methods.
checkbox.checked = false;
checkbox.selected = false;
checkbox.value = false;
A: 

Maybe:

checkbox.checked = "checked";

Then:

checkbox.checked = "unchecked";
Lucas Jones
Have just tried that, it doesn't work either.
Daniel
OK. I've no experience with Dashcode, but it looks like HTML. :) Hopefully someone more experienced will be able to help you here.
Lucas Jones
+1  A: 
checkbox.setAttribute("checked", "checked"); // set
checkBox.removeAttribute("checked"); // remove
Jaanus
Nope, that isn't working either.
Daniel
+1  A: 

Dashboard Widgets just run on WebKit technologies, so code valid for Safari should also be valid in Dashcode. Either of the following should work:

checkbox.checked = true;
checkbox.setAttribute("checked", "true");

The fact that they are not working indicates there is a problem elsewhere in your code. I would check the line

var checkbox = document.getElementById("checkbox");     

Correctly assigns an element to the checkbox variable. Also, check the id of your "checkbox" element is valid and correct (not a duplicate, doesn't have a typo, etc).

Andy E
Yeah, I've thought that it might be that, but I've quadruple checked, the ID is "checkbox" and the code is checkbox = document.getElementById("checkbox");I don't get any errors on runtime. I am running it in a stacklayout, would that change anything?
Daniel
This is the answer, particularly the first version (no need to go anywhere attributes). If setting the `checked` property isn't working then the fault lies elsewhere.
Tim Down
I just ran a test, its setting the checked property, however its not updating the checkbox. Also, through some more tests, initially it says the checked variable is undefined.
Daniel
+1  A: 

Hello! This question is one month old as I write this answer. It was probably already solved, but in any case I would like to add that if you are using Dashcode, the Checkbox part is a div which contains one label and one input, this one being the "real" checkbox.

If you inspect the html as it is loaded in Safari you will notice that "checkbox" is the type of the element.

Therefore the proper way to change the state of the checkbox would be, assuming "input" is its id (it could have a default number attached though):

document.getElementById("input").checked="true";

or whichever method you want to use.

The main point here is that you were trying to change the state of another div.

Hope it helps! Regards,

Rogelio.

Rogelio
This is correct. The OP is probably trying to change the state of the parent DIV element to 'checked'.
mclaughlinj