Getting all the switches in a form using prototype or regular JavaScript?

I have a simple form that includes many switches. Some of them are selected, some of them are not. Is there a way to get them all and set them to not be checked. I think that the setting is not checked, it can be done as follows:

button.checked = false; 

Question: how can I get all the buttons?

Thanks!

+4
source share
4 answers
 $$('input[type="radio"]:checked').each(function(c){ $(c).checked = false; }); 
+5
source

Your question is tagged and . If you want more syntactic sugar from PrototypeJS then the answer might look like

 $$('input[type="radio"]:checked').invoke('setValue', false); 

translating from Prototype into English sounds like invoke setValue(false) operation on all checked radio buttons .

You can use a somewhat similar design to search in one form.

 $('yourFormId').select('input[type="radio"]:checked').invoke('setValue', false); 

If you need plain old IE6-compatible JavaScript, then the answer will be

 var inputs = document.getElementsByTagName('input'); for (var i = 0, len = inputs.length; i < len; ++i) { if (inputs[i].type === "radio") { inputs[i].checked = false; } } 
+6
source
 document.querySelectorAll( 'input[type="radio"]' ); 

This will return a NodeList with all <input> elements of type radio. Then you can cross the list to change the checked attribute for everyone.

+2
source

What do you think about:

 var inputs = document.getElementsByTagName("input"); for(var i = 0; i < inputs.length; i++) { if(inputs[i].type == "radio") { inputs[i].checked = false; } } 

Please note that getElementsByTagName is not supported in some older IEs ...

+1
source

All Articles