get Selected radio button value??

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

  1. #1
    hii

    I have a 2 radio button in first.html So i want to get the selected radio button value using DOM APIs.
    without getElementById is preferable for me bcoz i am using node.type=="radio"

    can anyone help me out??
     
    sheriek, Oct 7, 2008 IP
  2. lp1051

    lp1051 Well-Known Member

    Messages:
    163
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #2
    Hi,
    in case you have radio as following : <input type="radio" name="yesno" value="yes" /><input type="radio" name="yesno" value="no" />

    You just go through all radios (in case of only two radios, you can just check if the first is checked) :
    var l = your_form.yesno.length;
    for(var i=0; i<l; i++) if(your_form.yesno.checked) var radio_value = your_form.yesno.value
    Where 'your_form' is the reference to the form, where the radio is

    Hope it helps
     
    lp1051, Oct 7, 2008 IP
  3. sheriek

    sheriek Guest

    Messages:
    125
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Please checkout i just want to get the selected value of the radio button.


    <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