Skip to content
Related Articles

Related Articles

How to get the value of radio button using AngularJS ?

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 27 Jan, 2022

In this article, we will see how to get the radio button’s value with the help of AngularJS. In this article, we will use the ng-model to get the value of the checked radio button. 

Example 1: In this example, the selected value is shown with an alert.

HTML




<!DOCTYPE HTML>
<html>
 
<head>
    <script src=
    </script>
     
    <script>
        var myApp = angular.module("app", []);
        myApp.controller("controller", function ($scope) {
            $scope.selectedGender = '';
            $scope.getVal = function (gender) {
                alert(gender);
            };
        });
    </script>
</head>
 
<body style="text-align:center;">
    <h1 style="color:green;">
        GeeksForGeeks
    </h1>
 
 
     
<p>
        How to get the radio button
        value in AngularJS
    </p>
 
 
 
    <div ng-app="app">
        <div ng-controller="controller">
            <form action="javascript:void(0)">
                Male: <input type="radio"
                    name="userGender"
                    ng-model='gender'
                    value="Male" />
                <br>
                Female : <input type="radio"
                    name="userGender"
                    ng-model='gender'
                    value="Female" />
                <br>
 
                <br>
                <button ng-click="getVal(gender)">
                    Click here
                </button>
            </form>
        </div>
    </div>
</body>
 
</html>

Output:

  • Before clicking on the button:

  • After clicking on the button:

Example 2: In this example, the selected value is shown in a <p> tag.

HTML




<!DOCTYPE HTML>
<html>
 
<head>
    <script src=
    </script>
 
    <script>
        var myApp = angular.module("app", []);
        myApp.controller("controller", function ($scope) {
            $scope.selGender = '';
            $scope.getVal = function (gender) {
                $scope.selGender = angular.copy(gender);
            };
        });
    </script>
</head>
 
<body style="text-align:center;">
    <h1 style="color:green;">
        GeeksForGeeks
    </h1>
 
 
     
<p>
        How to get the radio button
        value in AngularJS
    </p>
 
 
 
    <div ng-app="app">
        <div ng-controller="controller">
            <form action="javascript:void(0)">
                Male: <input type="radio"
                    name="userGender"
                    ng-model='gender'
                    value="Male" />
                <br>
                Female : <input type="radio"
                    name="userGender"
                    ng-model='gender'
                    value="Female" />
                <br>
 
                <br>
                <button ng-click="getVal(gender)">
                    Click here
                </button>
            </form>
 
             
<p>Chosen Gender = {{selGender}}</p>
 
        </div>
    </div>
</body>
 
</html>

Output:


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!