How to get the value of radio button using AngularJS ?

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

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>Choosen Gender = {{selGender}}</p>
        </div>
    </div>
</body>
  
</html>

chevron_right


Output:

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.