How To Get Value of Selected Radio Button Using jQuery

In this article we will see How To Get Value of Selected Radio Button Using jQuery. :checked selector and .val() method can be used to read value of radio button.

:checked selector

The :checked selector is used to select all checked checkboxes, selected radio buttons, and all options of select element.

Syntax

A simple example of :checked selector is shown below.

var length = $( "input:checked" ).length;

val() method

The .val() method is used to get the value of input elements like radio button, select element and textarea element. The .val() method does not require any arguments.

Syntax

A simple example of .val() method is shown below.

var Value = $( "#age" ).val();

Example 1

This example alerts the value of the selected radio button.

<form>
        <div>
            <input type="radio" name="car" value="Ford" id="orange">
            <label>Ford</label>
        </div>
        <div>
            <input type="radio" name="car" value="Jeep" id="apple">
            <label>Jeep</label>
        </div>
        <div>
            <input type="radio" name="car" value="Tesla" id="banana">
            <label>Tesla</label>
        </div>
</form>
    
<script>
        $('input[name="car"]').on("click", function() {
            var car = $('input[name="car"]:checked').val();
            alert(car);
        });
</script>

Demo

Example 2

This example also alerts the value of the selected radio button when the button is clicked.

<p>Your Gender?</p>
<input type="radio" name="gender" value="male">
<label>Male</label>
<input type="radio" name="gender" value="female">
<label>Female</label>
<input type="button" value="Get Gender">
    
<script>
   $(document).ready(function() {
   $("input[type='button']").click(function() {
   var gender = $("input[name='gender']:checked").val();
   alert(gender);
     });
   });
</script>

Demo

Your Gender?

Example 3

This example will change the background-color of the Demo div to the selected color using value of radio button.

<form>
        <div class="form-group">
            <label><strong>Select Your color</strong>:-</label><br>
            <label><input type="radio" name="color" value="red">Red</label>
            <label><input type="radio" name="color" value="yellow">Yellow</label>
            <label><input type="radio" name="color" value="blue">Blue</label>
            <label><input type="radio" name="color" value="orange">Orange</label>
        </div>
        <div class="form-group text-center">
            <button class="btn" type="button">Apply Color</button>
        </div>
</form>
<div id='div' style='padding:30px 10px;'>Demo</div>
    
<script type="text/javascript">
        $('.btn').click(function() {
            var value = $('input[name="color"]:checked').val();
            $('#div').css('background', value)
        });
</script>

Demo



Demo

Feel free to copy the code of any example and use it in anywhere.

Video Tutorial

Watch video tutorial on How To Get Value of Selected Radio Button Using jQuery.

Check If Input Field is Empty in jQuery jQuery Select All HTML Elements Whose ID Start With Same String Count Checked Checkboxes of Specific Form with JQuery Enable and Disable Button using JQuery Add Class to HTML Tag On Click Using Jquery Change Class of HTML Tag Using JQuery Display Current Date and Time Using HTML and JQUERY Detect Change in Text Input using JQuery Refresh a Page With JQuery Execute a Function on Hover Using JQuery Open Select File Dialog Box Using JQuery Display Message When File Is Selected Using JQuery Get Class of Clicked Element Using JQuery Remove White Spaces from Start and End of String using JQuery First Digit must be 0 using JQuery OnClick Close and Open Effect using JQuery Display clicked HTML Tag Name using JQuery Delay a Function Execution using JQuery Get ID of Clicked Element using JQuery Change HTML Button Text using JQuery