i have following radio button structure ...
<div id="test">
<input name="test1" value="a" type="radio">
<input name="test1" value="b" type="radio">
<input name="test1" value="c" type="radio">
</div>
how would i go about retrieving the value of any checked radio button?
i have checked the YUI documentation an there is not really any good example.
I would also like to know how to get the element by input name in YUI?
To find the selected radio button, you follow these steps: Select all radio buttons by using a DOM method such as querySelectorAll() method. Get the checked property of the radio button. If the checked property is true , the radio button is checked; otherwise, it is unchecked.
One solution is to assign on mousedown the value you are going to assign to it (the opposite of what it has) in a variable on the node like this. __rval and check for its existence in your onclick handler. If it exists, you know the value in it is correct, though the this.
Use document. getElementById('id'). checked method to check whether the element with selected id is check or not. If it is checked then display its corresponding result otherwise check the next statement.
The only thing you have to change in your code is the name of the radio button because all radio buttons with the same name are assumed to be a group, and the web-browser only allows selection of only one of them by default, and you cannot control that.
In YUI 3:
var value = Y.one("#test input[name=test1]:checked").get("value");
In YUI 2:
// the null, null, null, true is optional, but returns only the first match
var input = YAHOO.util.Dom.getElementsBy(function (el) {
return (el.name === 'test1' && el.checked);
}, 'input', 'test', null, null, null, true);
var value = input.value;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With