how to get radio button value in javascript--

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • selvialagar
    New Member
    • Apr 2008
    • 57

    how to get radio button value in javascript--

    my code doesn't work. undefined is displayed.....h ow to get a value of radoi button in javascript

    [HTML]function enableaddr()
    {
    alert(document. getElementById( 'reason').lengt h);
    for (var i=0; i < document.getEle mentById('reaso n').length; i++)
    {
    if (document.getEl ementById('reas on[i]').checked)
    {
    var reason1 = document.getEle mentById('reaso n[i]').value;
    alert(reason1);
    }
    }
    }

    <form action="cust_in dex.php?action= delete&cid=<?ph p echo $cid;?>" name="del_form" >
    <table width="400" border="0" cellpadding="6" align="center" cellspacing="1" class="bg1">
    <tr class="bg2"><td >Enter the reason to delete the record with the customer name <?php echo $name;?> :</td></tr>
    <tr class="bg2"><td ><input type="radio" name="reason" id="reason" value="close" onclick="enable addr()" />Close</td></tr>
    <tr class="bg2"><td ><input type="radio" name="reason" id="reason" value="shifta" onclick="enable addr()" />Shift &nbsp;</td></tr>
    <tr class="bg2"><td >Enter the address :&nbsp;&nbsp;&n bsp;<textarea cols="30" rows="4" disabled="disab led" name="shiftaddr ess" id="shiftaddres s"></textarea></td></tr>
    <tr class="bg2"><td align="center"> <input type="submit" value="submit" class="button"> </td></tr>
    <input type="hidden" name="reason1" id="reason1" />
    </table>
    </form>[/HTML]
    Last edited by gits; Apr 2 '08, 09:05 AM. Reason: added code tags
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5388

    #2
    you make the wrong use of getElementById( ). first, the id HAS TO be unique in a page ... and so the method could give you just ONE element. you may use something that retrieves you a node list like in the following example:

    [CODE=javascript]function enableaddr() {
    var reasons = document.getEle mentsByName('re ason');

    for (var i = 0; i < reasons.length; i++) {
    if (reasons[i].checked) {
    var reason1 = reasons[i].value;
    alert(reason1);
    }
    }
    }
    [/CODE]
    kind regards

    Comment

    Working...