Radio button problem

Results 1 to 2 of 2

Thread: Radio button problem

  1. #1
    Join Date
    Dec 1969

    Default Radio button problem

    I&#039;ve got a number of radio buttons on a form, depending on the number of rows returned in a query. The radio buttons allow a user to edit/delete a selected record. I use a JS function to validate that the user has selected a radio button when a button is clicked. e.g. if the user clicks the Delete button ...<BR><BR> case "D" : <BR> if ((getRadioGroupValue( document.frmSavings.radSaveID ) =="")){ <BR> alert("Select a Saving to Delete ...."); <BR> return false;} <BR><BR> if (confirm("Delete the Selected Saving ?")) { <BR> document.frmSavings.action = "SavingsUpdate.asp?Mode="+"D"<BR> document.frmSavings.submit();<BR> window.close();<BR> }<BR> break;<BR><BR>the getRadioGroupValue function is as follows<BR><BR>function getRadioGroupValue( group ) <BR>{ <BR>alert(group.length)<BR> for ( var i = 0; i &#060; group.length; ++i ) <BR> <BR>{ <BR> if ( group[i].checked ) return group[i].value; <BR> <BR>} <BR> return ""; } <BR><BR>the problem is if the query only returns only one record I keep getting an alert to "Select a Saving to Delete ...." and the alert(group.length) returns [undefined]. , whereas fi ther are teo records the alert(group.length) returns [undefined] returns 2. Any ideas how I get around this ?<BR><BR>Many Thanks<BR>Ray

  2. #2
    Join Date
    Dec 1969

    Default Yep...same problem... would have with one/multiple checkboxes of same name.<BR><BR>Just do this:<BR><BR>function getRadioGroupValue( group ) <BR>{ <BR> if ( group.length == null ) // happens when only 1 button in group!<BR> {<BR> return group.checked; // it&#039;s not really a group, then!<BR> }<BR> for ( var i = 0; i &#060; group.length; ++i ) <BR> { <BR> if ( group[i].checked ) return group[i].value; <BR><BR> } <BR> return ""; <BR>} <BR><BR>The DOM automatically converts multiple form fields of the same name to an array (and an array has a .length property, which is why we test for it). But if there is only one field of the name, no array is generated (and thus there is no .length property).<BR><BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts