Snippets v2

Languages
My Snippets

Check if one radio option was chosen Login or Register to Bookmark this snippet

Description Javascript

This function checks that a radio option was selected from a group. Handy for form validation.

Requires xGetElementsByTagName (also available on this site).

The Code Download
  1. function checkOneRadioSelected(elm,msg) {
  2.     if (elm) {
  3.         if(typeof(elm)=='string') {
  4.             elm = xGetElementById(elm);
  5.         }
  6.         var f = searchUp(elm,'form');
  7.         var ins = xGetElementsByTagName('input',f);
  8.         for (i=0;i<ins.length;i++) {
  9.             if (ins[i].type.toLowerCase() == 'radio' && ins[i].name == elm.name && ins[i].checked) {
  10.                 return true;
  11.             }
  12.         }
  13.         if (msg) {
  14.             alert(msg);
  15.         }
  16.     }
  17.     return false;
  18. }
Credits Contact JC

Added by JC on 12th November, 2007

Comments

There are no comments about this snippet.

Post Comment HTML is allowed

You must be registered and logged in to post a comment.

Login here to post a comment