Finding The Value Of Selected Radio Button

Finding The Value Of Selected Radio Button Example describes about How to find the selected radio button.

For getting the value of checked radio button, We must iterate all radio buttons in that array and find the selected one

If the radio buttons are more than one, it is consider as an array, but if only one radio button exists, it will not treat as array. so avoid this problem, we need to add hidden choice.

Getting / Finding The Value Of Selected Radio Button

<html>
<head>
    <script language="JavaScript">
        function getRadioButton() {
            for (index=0; index < document.radioButton.choice.length; index++) {
                if (document.radioButton.choice[index].checked) {
                    var radioButton = document.radioButton.choice[index].value;
                    alert(radioButton);
                    break;
                }
            }
        }
    </script>
</head>

<body>
<form name="radioButton" method="post" onclick="getRadioButton()">
<input name="choice" value="-1" type="hidden" />
<input name="choice" value="1" type="radio" />1 
<input name="choice" value="2" type="radio" />2 
<input name="choice" value="3" type="radio" />3 
<input name="choice" value="4" type="radio" />4
    </form>
</body>
</html>

 











Your email address will not be published. Required fields are marked *