views:

2138

answers:

2

Question 1: Given

<input type="radio" name="foo" value="1" />
<input type="radio" name="foo" value="2" />
<input type="radio" name="foo" value="3" />

In Mootools, how do I return "2" given an input of "foo", assuming that the second radio button was clicked.


Question 2: (it's related)- Given similar checkbox inputs, how do I return either an array or comma separated list of the checked values?

I'm wanting to submit the values of these inputs via Request.JSON, passing it as a GET parameter.

+2  A: 

Assuming the name of the checkbox is "foo", to get the selected radio item, you can use:

var values = $$('input[name=foo]:checked'​​​​​​​​​​​​​​).map(function(e) { return e.value; });

This returns an array with 1 item, being the selected elements' value.

Or just extend an Array's prototype, and add a getFirst() method.

Array.implement({
    getFirst: function() {
        if(this.length == 0) {
            return null;
        }
        return this[0];
    }
});

Then you could do this:

var value = $$('input[name=foo]:checked').getFirst().value;

Similarly, to get all checked checkboxes, use:

var values = $$('input[name=foo]:checked'​​​​​​​​​​​​​​).map(function(e) { return e.value; });

The double dollar ($$) function is used to select multiple elements with a CSS selector. Then a map (part of Array class) can be applied to those elements to get just the needed value(s).

You can read more about these pseudo-selectors at http://mootools.net/docs/core/Utilities/Selectors

Anurag
for singular use, why not just use `formel.getElement("input[name=foo]:checked")` - no need for array dealings here. i'd take that approach and its more scalable and loop through multiple radios with this as callback.
Dimitar Christoff
i used $$ as the OP only had the field name in the question. But if the form element is available or it's id is known, then we can also use `$('formID').getElement('input[name=foo]:checked").value`
Anurag
Thanks. I used `$('formID').getElement('input[name=foo]:checked").value` for the radio (which could only have one value) and `$$('#formID input[name=foo]:checked')` for the checkboxes.
philfreo
And to get an array of the checked values, I did the following. LMK if there's a more elegant way.`var vals = []; $$('#formID input[name=ubers]:checked').map(function(e) { vals.push(e.value); });`
philfreo
@philfreo if you're pre-defining an array, then you should use each to iterate instead of map. map created an implicit array and since you're not returning anything from the mapper function (each value in that array is undefined). so either of these should be better: `var vals = $$('#formID input[name=ubers]:checked').map(function(e) { return e.value; });` or with predefining an array: `var vals = []; $$('#formID input[name=ubers]:checked').each(function(e) { vals.push(e.value); });`
Anurag
Makes sense... thanks - I get the map function now.
philfreo
A: 

no,no,no... how to get the radio's value,the right answer is: var value =$$('input[name=type]:checked').get('value'); how easy....

weiqianglufromchina