Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting all radio buttons in a form using Prototype or plain JavaScript?

I have a simple form which includes lots of radio buttons. Some of them are selected, some of the are not. Is there a way to get them all and set them to not checked. I guess setting to not checked can be done like this:

button.checked = false;

Question is, how do I get all the buttons?

Thanks!

like image 576
user1856596 Avatar asked Dec 08 '22 18:12

user1856596


2 Answers

Your question is tagged both javascript and prototypejs. If you want more syntactic sugar from PrototypeJS, then answer may look like

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

translation from Prototype to english sounds like invoke setValue(false) operation on all checked radio buttons.

To search in one form you can use somewhat similar construction

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

If you want plain old IE6-compatible JavaScript, then 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;
  }
}
like image 191
Victor Avatar answered Dec 11 '22 08:12

Victor


$$('input[type="radio"]:checked').each(function(c){
    $(c).checked = false;
});
like image 24
John Conde Avatar answered Dec 11 '22 09:12

John Conde