AJAX Page - Knowledge Base

« Back to Knowledge Base list

Getting the value of checked radio button

Q:

How to get the value of checked radio button in client-side for posting to server-side?

A:

This client-side code block can be used:

<input name="FindBy" type="radio" value="QN" checked="checked" />QueryName

<input name="FindBy" type="radio" value="QID" />QueryID

<input name="FindBy" type="radio" value="AID" />AdvertiserID

<input name="FindBy" type="radio" value="UN" />UserName

<script type="text/javascript">
       
function GetCheckedValue(){
            var chkElement = '';
            var radioButtons = document.getElementsByName('
FindBy');
            for(var i = 0; i < radioButtons.length; i++)

            {
                    var elem = radioButtons[i];
                    if(elem.checked)
                    {
                        chkElement = elem.value;
                        break;
                    }
            }
            return chkElement;
        }
       
        function ServerLookup()
        {
            ob_post.AddParam("FindBy", GetCheckedValue());
            ob_post.post(null, "loadData", "EndLookup");
        }

</script>

The client-side variable chkElement will hold the value of the checked element, which can be passed server-side.