Get / set flags and values โ€‹โ€‹of radio buttons using a prototype

The prototype is wonderful, but with the release of 1.6.1, the library still does not allow receiving / configuring grouped inputs (checkboxes and radio buttons). I would like to get an array of selected values โ€‹โ€‹using $F($("form").checkboxes) . I would like to set these checkboxes to an array of values, on the other hand.

Ideas?

+4
source share
5 answers

In the end, I wrote my own Prototype extensions for this. You can see them on Github . Here's a note from the project:

โ€œThese extensions allow you to use the prototype-friendly syntax $ F () to get and set the values โ€‹โ€‹of these grouped input elements. They have been tested using Prototype 1.6.0.3 and 1.6.1. There are other bits in these extensions. See the README file for more details . html

The answers from sawgee and Los are much appreciated, but I need a more integrated solution that would allow me to work with checkboxes and radio buttons with the natural $ F () syntax, which I already use with other form elements.

+1
source

You can always do something like this: (assuming your checkboxes have the checkboxes class).

 var checkedList = []; $$('.checkboxes').each(function(ele){ if( $(ele).checked ) { checkedList.push($(ele).name); } }); 
+9
source

edit - just got it, I read the question incorrectly, the code below is only good for setting values:

 var form = $('options'); checkboxes = form.getInputs('checkbox'); checkboxes.each(function(e){ e.checked = 0 }); // or even checkboxes.invoke('checked',0); 

can use something like this:

 var cels = new Array(); $$('checkbox[checked="checked"]').each(el){ cels.push(el.value); } 
+6
source

This is only for switches, but still useful.

Get

 $$('input:checked[name="radio_name"]')[0].value 

Set

 $$('input[name="radio_name"][value="to_select"]')[0].checked = true 

"radio_name" is the name of your radio group. "to_select" - whatever value you choose. "[0]" works because only 1 radio book can be checked at a time.

On the side of the note, I don't seem to like the prototype. JQuery is much simpler, but you should use what you need to use.

+4
source

Given HTML:

 <label><input type="radio" id="choiceA" name="choices" value="A" checked="checked" /> A</label> <label><input type="radio" id="choiceB" name="choices" value="B" /> B</label> 

This makes a request for CSS styles and returns a value for all elements.

 $$('input:checked[type="radio"][name="group-name"]').pluck("value"); 
0
source

All Articles