Get selected value of radio button using YUI 3
Getting the value of a the currently selected radio button may be done in a couple of ways. This is one approach using YUI 3.
Here is the HTML:
<input type="radio" name="myoptions" value="one"> First <input type="radio" name="myoptions" value="two"> Second <input type="radio" name="myoptions" value="three"> Third
Get the currently selected value using only one line of code:
var value = Y.one('[name=myoptions]:checked').get('value');
Unfortunately, this does not work in Internet Explorer, so another (not as tight) approach is:
function getRadioSelectedValue(selector) {
var tmpValue= false;
Y.all(selector).each(function(node) {
if (node.get('checked') tmpValue = node.get('value');
});
return tmpValue;
}
var value = getRadioSelectedValue('.myRadioGroupClass'); // this assumes you've set appropriate class to radio elements
Your one-liner will work in IE if you include the ‘selector-css3′ module.