Posts Tagged ‘checkbox’

Dojo checkboxes

Thursday, July 3rd, 2008

I recently hit this snag when working with Dojo. Basically I wanted to set the checked status of a checkbox on a webpage programmatically. Simple you might think? Apparently not as easy as it should be.

Interaction with checkboxes has changed slightly in Dojo 1.1 (apparently) but myCheckbox.setValue(true) should be valid. When calling dojo.byId('my-checkbox-id').setValue(true), I was getting an error saying the method didn’t exist. The object was definitely the checkbox as I could determine the correct checked state from that object (myCheckbox.checked) so I was very confused. I then remembered another way to access objects with Dojo is using the HTML attribute ‘jsId‘. This creates a global javascript variable referring to that object in the DOM. So I set something like jsId='myCheckbox' and then called myCheckbox.setValue(true) and it worked!

Very odd behaviour. I can only guess that the javascript object created by Dojo using jsId and dojo.byId() is a different bit of code and creates an object pointing to the DOM checkbox object in a different way. Very strange but at least there’s the workaround above…