Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access the value of a radio button that is checked using YUI?

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?

like image 488
mahatmanich Avatar asked Dec 07 '10 11:12

mahatmanich


People also ask

How do you check the radio button is checked?

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.

How do you checked a radio button when a button is clicked?

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.

How do I check if a radio button is checked in HTML?

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.

Can radio button have same value?

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.


1 Answers

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;
like image 190
Luke Avatar answered Sep 17 '22 17:09

Luke