Friday, 27 May 2011

Getting and Setting RadioButton List in JavaScript on ASP.NET 2.0

I am by no means a JavaScript expert  but had to get my feet wet as I couldn't find a working piece of code that allowed an update of radio button list via JavaScript.

There are two JavaScript Functions:  getRadVal will get the value of a RadioButtonList and  setRadVal will set the value of value of a RadioButtonList.

function getRadVal(radlist)
if (document.forms['Form1'].elements[radlist])
{ var radGrp = document.forms['Form1'].elements[radlist];
var radGrpValue = '0';
for (var i = 0; i < radGrp.length; i++) 
   if (radGrp[i].checked) {
       radGrpValue = radGrp[i].value;
return radGrpValue;
return '';

function setRadVal(radlist, newValue) {
    if (document.forms['Form1'].elements[radlist]) {
        var radGrp = document.forms['Form1'].elements[radlist];
        for (var i = 0; i < radGrp.length; i++) {
            radGrp[i].Checked = false;
            var evalue = radGrp[i].value         
            if (evalue == newValue) {
                radGrp[i].checked = true;
                radGrp[i].value = newValue;               

These are called as so

// Get Value of RadioButtonList
var myValue=getRadVal('UCMAIN:RadioList1')

//Set Value of RadioButtonList
setRadVal('UCMAIN:RadioList2', myValue)

Above example syncs too RadioButtonLists with the same value.

No comments:

Post a Comment

Comments are welcome, but are moderated and may take a wee while before shown.