DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Select One Option In Radio Button

07.04.2005
| 8031 views |
  • submit to reddit
        // Nota  : Rafael Trindade da Silva - Mantus Deus Estrusco do Inferno
// Email : rafaeltds at gmail dot com

function isRadio(camporadio) {
 var tamanho = camporadio.length;
 var retorno = false;
 for (var cont = 0; cont < tamanho; cont++) {
  if(camporadio[cont].checked) {
    retorno = true; break;
  }
 }
 return (!retorno) ? false : true;
}
    

Comments

Snippets Manager replied on Mon, 2012/05/07 - 2:13pm

Why? Radio with only one item is not a special case and will get handled in exactly the same way as multiple-option one. As far as I can see this function always returns true, if radio option was selected and false otherwise. It's just not a very direct way to do this. For example condition in return says basically, if retorno is false, return false and true when it's true. So why not just return retorno?

Snippets Manager replied on Mon, 2012/05/07 - 2:13pm

if the radio has only one item, you'll face some shit :D you can remove the if by adding a second statement inside the "for" loop-checking ;]