Radio Buttons JavaScript

Results 1 to 2 of 2

Thread: Radio Buttons JavaScript

  1. #1
    Join Date
    Dec 1969

    Default Radio Buttons JavaScript

    I have two radio buttons. When you select one it shows another bit of the form and hides another. I want to be able to say the following in JavaScript:<BR><BR>If the radio button FinancialCode is checked {<BR>do validation on fields that are displayed now<BR>}<BR><BR>If the radio button ProjectNumber is checked {<BR>do validation on fields that are displayed now<BR>}<BR><BR>I do not know how to check for which radio button is checked.<BR><BR>I have included my radio button HTML. Thanks,<BR>Jamie<BR><BR>&#060;label for="FinancialCodeSelect" &#062; <BR> &#060;input name="fin_prono" type="radio" id="FinancialCodeSelect" onclick="toggleFinancialCode()" value="FinancialCode" checked&#062;<BR> Financial Code &#060;/label&#062;<BR> <BR><BR> &#060;label for="ProjectNumberSelect"&#062; <BR> &#060;input name="fin_prono" type="radio" id="ProjectNumberSelect" onclick="toggleProjectNumber()" value="ProjectNumber"&#062;<BR> Project Number&nbsp;&#060;/label&#062;

  2. #2
    Join Date
    Dec 1969

    Default Several ways...

    &nbsp;<BR>if ( document.FormName.fin_prono[0].checked )<BR>{<BR> ... process financial stuff ...<BR>} else {<BR> ... process project stuff ...<BR>}<BR><BR>Or you coud write a function that gets the value of the checked radiobutton and make the if test based on that:<BR><BR>function rbValue( rbgroup )<BR>{<BR> for ( var i = 0; i &#060; rbgroup.length; ++i )<BR> {<BR> if ( rbgroup[i].checked ) return rbgroup[i].value;<BR> }<BR> return null;<BR>}<BR><BR>if ( rbValue( document.FormName.fin_prono ) == "FinancialCode" )<BR>{<BR> ... process financial stuff ...<BR>} else {<BR> ... process project stuff ...<BR>}<BR><BR>Since you are likely to find many uses for that rbValue function in the future, might as well invest the time to use it now.<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