radio button

Discussion in 'JavaScript' started by sheriek, Oct 7, 2008.

  1. #1
    I want to get selected radio button value.Here is my small code.please look at this and give me the way to get the selected value.

    <script language="javascript">
    function ViewPopup(){

    myWindow=window.open('','','width=400,height=200');

    var table =document.getElementById("myTab");
    var cells = table.getElementsByTagName("td");

    for (var i = 0; i < cells.length; i++) {
    node=cells.firstChild;
    // Here is problem
    if(node.type=="radio"){
    if (cells.checked=true){
    myWindow.document.writeln(node.getAttribute("value")+"<br/>");
    }
    }
    }
    myWindow.focus()

    }

    </script>
    <body>
    <table id="myTab" width="200" border="1">
    <tr>
    <td colspan="2">
    <input type="button" name="btn" value="Data" onclick="ViewPopup()"/> </td>
    </tr>

    <tr>
    <td colspan="2">
    <input type="radio" name="marital" value="single" id="marital_single" />
    Single<br />
    <input type="radio" name="marital" value="divorced" id="marital_divorced" />
    Divorced</td>
    </tr>
    </table>
    </body>
     
    sheriek, Oct 7, 2008 IP
  2. jgarrison

    jgarrison Peon

    Messages:
    66
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    This will do it:

    <script language="javascript">

    function ViewPopup(){
    myWindow=window.open('','','width=400,height=200');
    for (i=0;i<document.myForm.marital.length;i++){
    if (document.myForm.marital.checked==true)
    myWindow.document.writeln(document.myForm.marital.value+"<br />");
    }
    myWindow.focus()
    }

    </script>

    <form name="myForm">
    <table id="myTab" width="200" border="1">
    <tr>
    <td colspan="2">
    <input type="radio" name="marital" value="single" id="marital" />
    Single<br />
    <input type="radio" name="marital" value="divorced" id="marital" />
    Divorced</td>
    </tr>
    <tr>
    <td colspan="2">
    <input type="button" name="btn" value="Data" onclick="ViewPopup()"/> </td>
    </tr>
    </table>
    </form>

    -Jim
     
    jgarrison, Oct 7, 2008 IP