Login Register

radiobutton: get selected value

After hours of searching a way to submit only the value of an radiobutton group, i find myself searching in the api. as far as i see, there is no simple way to access the checked (selected) value. So i just hacked into the Checkbox.js following code:

getCheckedValue: function(){
			// HACK
			var res = false;
			dojo.forEach(this._groups[this.name], function(widget){
				if(widget.checked) res = widget.value;
			}, this);	
			return res;
		},

It works so far. Maybe its a nice extension for the next release...
You can call it by:

dijit.byId('radio1').getCheckedValue()

where "radio1" is the id of first element of the group. i was really wondering, why you
save an internal group array by name and dont use it then...

greetings from germany,
jakob

hi Jakob

Yeah, I agree that the current interface is unwieldy, but (as you might guess) we built it to parallel that way native HTML radio buttons work. Of course that doesn't mean we couldn't add the feature you suggested too.

I've also thought about making a RadioGroup widget (which contains radio buttons, and probably labels too)... basically a souped-up fieldset.

BTW, your could use dojo.filter() instead of dojo.forEach(), like

var matches = dojo.filter(this._groups[this.name], "item.checked");
return matches[0];

(that's using the tricky shorthand where you pass a string and it turns it into a function for you).

And finally, note that the dijit.Form widget can also figure out the value for you automatically, and other nice features too. But that may well be overkill for whatever you are doing.

=========
Bill Keese
Project Lead (aka BDFL) of Dijit

thanks

thanks for the fast answer. i like the idea of the RadioGroup widget.
But how can the dijit.Form get the value automatically for a set of
radiobuttons with the same name? .getValue() refers to one element,
but not to the group...

i just want to submit the selected value to the server with one simple
xhrPOST call.

greetings, jakob

p.s: with this toolkit you did all a very good job. tested some frameworks
for php, but this one is really great. (at least so far ;-) )

no problem

Glad you like the toolkit.

dijit.Form has some complicated code that queries every single widget the form contains, and then returns the results as a javascript object. See Form.html, hit the "get values" button and then see the console.

=========
Bill Keese
Project Lead (aka BDFL) of Dijit