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.

Demo

Video Tutorial

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

Check All Checkboxes with jQuery Uncheck All Checkboxes with jQuery Toggle Class OnClick using jQuery Remove id from HTML Tag Using jQuery Detect Shift Keypress with jQuery Remove Class from HTML Tag Using jQuery Delete Last List Element with jQuery Delete First List Element with jQuery Highlight Table Row On Hover using JQuery Toggle Button Text with jQuery Uncheck all Radio Buttons using jQuery Select Option By Index with jQuery jQuery Make Textarea Readonly Detect Enter Key Press in Textarea with jQuery Disable Textarea Resize Property with jQuery Clear Textarea Text with jQuery Get Textarea Tag Value in jQuery Count Characters of Textarea in jQuery Change value of href attribute of link 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