Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get/Set Checkbox and Radio Button values using Prototype

Prototype is great, but with the 1.6.1 release, the library still doesn't allow getting/setting of grouped inputs (checkboxes and radio buttons.) I'd like a way to get an array of selected values with $F($("form").checkboxes). I'd like to be able to set those checkboxes to an array of values, on the flip side.

Ideas?

like image 534
Eric Nguyen Avatar asked Nov 30 '22 06:11

Eric Nguyen


2 Answers

You can always do something like this: (assumes you have your checkboxes have a class of checkboxes).

var checkedList = [];
$$('.checkboxes').each(function(ele){
   if( $(ele).checked )
   {
       checkedList.push($(ele).name);
   }
});
like image 95
Los Avatar answered Dec 15 '22 11:12

Los


edit - just realised i misread the question, code below 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);

could maybe use something like this though:

var cels = new Array();
$$('checkbox[checked="checked"]').each(el){
  cels.push(el.value);
}
like image 39
robjmills Avatar answered Dec 15 '22 09:12

robjmills