Problem with getting radio button value

Discussion in 'JavaScript' started by Jawn, Nov 8, 2008.

  1. #1
    Hello, im having a bit problem getting the radio button value, here's the code


    
    <SCRIPT LANGUAGE="JavaScript"><!--
    
    function changeForm(what) {
        for (var i=0; i<what.options.length; i++) {
            if (what.options[i].selected) {
                if (document.all)         document.all[what.options[i].value].style.visibility="visible";
                else if (document.layers) document.layers[what.options[i].value].visibility = "visible";
            }
            else {
                if (document.all)         document.all[what.options[i].value].style.visibility="hidden";
                else if (document.layers) document.layers[what.options[i].value].visibility = "hidden";
            }
        }
    }
    
    //--></SCRIPT>
    
    <FORM name="orderform" id="orderform">
    Form1: <input type="radio" value="form1" name="persontype" onChange="changeForm(this.document.orderform.persontype[0].checked)"> 
    Form2: <input type="radio" value="form2" name="persontype" onChange="changeForm(this.document.orderform.persontype[0].checked)">
    </FORM>
    
    
    <DIV STYLE="position: absolute">
    </DIV>
    
    <DIV ID="form1" style="position: absolute; top: 100; left: 100; visibility: visible;">
    <FORM>
    Form 1 - <INPUT TYPE="TEXT" NAME="myName1" VALUE="myValue1">
    </FORM>
    </DIV>
    
    <DIV ID="form2" style="position: absolute; top: 100; left: 100; visibility: hidden;">
    <FORM>
    Form 2 - <INPUT TYPE="TEXT" NAME="myName2" VALUE="myValue2">
    </FORM>
    </DIV>
    
    <DIV ID="form3" style="position: absolute; top: 100; left: 100; visibility: hidden;">
    <FORM>
    Form 3 - <INPUT TYPE="TEXT" NAME="myName3" VALUE="myValue3">
    </FORM>
    </DIV>
    
    Code (markup):
    this.document.orderform.persontype[0].checked doesnt seem to work or maybe i have forgotten something.
     
    Jawn, Nov 8, 2008 IP
  2. cont911

    cont911 Peon

    Messages:
    50
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    1. use onlick and pass selected radio button value using 'value' property
    <FORM name="orderform" id="orderform">
    Form1: <input type="radio" value="form1" name="persontype" onclick="changeForm(this.value)">
    Form2: <input type="radio" value="form2" name="persontype" onclick="changeForm(this.value)">
    </FORM>

    2. use changeForm function idea below
    function changeForm(what)
    {
    if(what == 'form1')
    {
    alert('show form1, hide form2');
    }

    if(what == 'form2')
    {
    alert('show form2, hide form1');
    }

    }
     
    cont911, Nov 9, 2008 IP