0

I've two radio buttons to select a type of IVA:

<input type="radio" value="18" checked="true" name="ivacontract"> 18%
<input type="radio" value="16" name="ivacontract"> 16%

And I've one function that will calculate a value depending of the value of the radio button with name "ivacontract". In jQuery I'll use a selector and .val(); to get the value but this application (not coded by me) doesn't use jQuery so I don't know how to do it.

Any help?

Thank you in advance!

udexter
  • 2,307
  • 10
  • 41
  • 58
  • possible duplicate of [How to I get the value of a radio button with javascript](http://stackoverflow.com/questions/3778206/how-to-i-get-the-value-of-a-radio-button-with-javascript) – Shadow The GPT Wizard Jan 27 '11 at 12:21

4 Answers4

2
function getCheckedValues(objName)
    {   
        var arr = new Array();
        arr = document.getElementsByName(objName);

        for(var i = 0; i < arr.length; i++)
        {
            var obj = document.getElementsByName(objName).item(i);

            if(obj.checked)
            {
                alert(obj.value);
            }
        }
    }

getCheckedValues("ivacontract");

Here is a working sample:

http://jsfiddle.net/eJBg9/1/

Aivan Monceller
  • 4,636
  • 10
  • 42
  • 69
1

Try this:

function checkradio(fname,rname)
{
    var radios=document[fname].elements[rname];
    for(var i=0;i<radios.length;i++)
    {
    if(radios[i].checked)
        return radios[i].value;
    }
    return false;
}

Where fname is your form name and rname is your radio buttons name that is in your code is ivacontract.

Hope this helps.

Harry Joy
  • 58,650
  • 30
  • 162
  • 207
0

Also, here is an example of getting and setting a value for the radio buttons.

http://www.somacon.com/p143.php

Arseny
  • 5,159
  • 4
  • 21
  • 24
0
<input type="radio" id="radio1" value="18" checked="true" name="ivacontract"> 18%
<input type="radio" id="radio2" value="16" name="ivacontract"> 16%


if (document.getElementById('radio1').checked==true) )
{
///   Your Code ///
}
Yahoo
  • 4,093
  • 17
  • 59
  • 85
  • Wat exactly is happening in your code. It seems very simple as well as tricky too. Can u please explain it. Thanks in Advance :P – suraj May 11 '12 at 08:49