Dojo checkboxes

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…

Advertisements

16 thoughts on “Dojo checkboxes

  1. You need to use dijit.byId instead of dojo.byId. Then you're in business without setting a jsId for every widget.

  2. Ah, thanks very much. For anyone interested, I've discovered the reason for this (copied from http://tinyurl.com/58oqey):Here's the difference: dijit.byId() returns a Dijit widget instance – technically, an instance of dijit._Widget or one of its subclasses like dijit.Toolbar or dijit.TreeNode.So here's the way to remember it: * If you need a DOM Node, use dojo.byId. You need a DOM node anytime you call a standard browser function, change a standard property, etc. * If you need a widget, use dijit.byId. From it you can access all of the attributes, methods and extension points listed in Part 2.Useful stuff to know when using digit widgets.

  3. You need to use dijit.byId instead of dojo.byId. Then you're in business without setting a jsId for every widget.

  4. Ah, thanks very much. For anyone interested, I've discovered the reason for this (copied from http://tinyurl.com/58oqey ):Here's the difference: dijit.byId() returns a Dijit widget instance – technically, an instance of dijit._Widget or one of its subclasses like dijit.Toolbar or dijit.TreeNode.So here's the way to remember it: * If you need a DOM Node, use dojo.byId. You need a DOM node anytime you call a standard browser function, change a standard property, etc. * If you need a widget, use dijit.byId. From it you can access all of the attributes, methods and extension points listed in Part 2.Useful stuff to know when using digit widgets.

  5. I totaly agree with your point here! I admire the stuff you post and the quality information you offer in your blog! Keep up the awesome work dude

  6. I also ran into a similar issue just now. As I've found out, using dijit.byId is for when you're using dojo widgets. As I wasn't, I had to work on the DOM level, so I used dojo.byId(“controlid”).checked to see if it was checked.

  7. I also ran into a similar issue just now. As I've found out, using dijit.byId is for when you're using dojo widgets. As I wasn't, I had to work on the DOM level, so I used dojo.byId(“controlid”).checked to see if it was checked.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s