How to get radio button value in jquery

Category: Slideshow | August 22, 2023

How to Get Radio Button Value in jQuery ?

Radio buttons are an input element that allows users to select only one item from a list. When working with forms and user interactions, retrieving the selected value of a radio button is a regular operation. This article will show you how to use jQuery to get the value of a radio button.

What does it mean by Retrieving Radio Buttons Value in jQuery?

Retrieving radio button values with jQuery refers to the process of finding out and gathering an option of choice from a group of radio buttons in a web form using the jQuery library, a useful tool for simplifying JavaScript code. Radio buttons are typically used when users must choose a single option from a list of options.
Developers may use jQuery to write code that monitors for user input with these radio buttons. When a user clicks a radio button to make a selection, jQuery assists in determining which button was pressed and retrieving the value associated with it. This value can be used to process form submissions, update web content, or trigger particular actions based on the user’s selection.

Syntax:

Syntax for jQuery code

$(selector).action();

$: Represents the jQuery library symbol, providing access to its functions.
selector: Identifies the HTML element(s) to be targeted, similar to how CSS selectors work.
action(): Denotes the specific jQuery action to be performed on the selected element(s), such as modifying content, applying styles, or handling events.

Getting the Values of Radio Buttons:

To get the value of a radio button using jQuery, you can use the following methods:

1. Getting Radio Selected Value Using Class

HTML:
Add the class=”fruit-radio” attribute to each radio button.

<input type="radio" name="fruit" class="fruit-radio" value="apple"> Apple <input type="radio" name="fruit" class="fruit-radio" value="banana"> Banana <input type="radio" name="fruit" class="fruit-radio" value="orange"> Orange

jQuery:
Then use the class selector .fruit-radio in the script.

$(document).ready(function() {
    $(".fruit-radio").on("click", function() {
        var selectedFruit = $(".fruit-radio:checked").val();
        console.log("Selected fruit using class: " + selectedFruit);
    });
});


  • Each radio button in the group has the class “fruit-radio” applied to it.
  • The.fruit-radio:checked selector is used to target the radio button with the class “fruit-radio” that is presently checked.
  • The .val() method retrieves the value of the selected radio button.

This is how the output is shown on the webpage:       

2. Getting Radio Selected Value Using Attribute Name with Value

In this approach, you’ll target the radio buttons using the [name=”fruit”] attribute selector and choose the checked one with :checked.

HTML:
Add the name=”fruit” attribute to each radio button.

<input type="radio" name="fruit" class="fruit-radio" value="apple"> Apple <input type="radio" name="fruit" class="fruit-radio" value="banana"> Banana <input type="radio" name="fruit" class="fruit-radio" value="orange"> Orange

jQuery:

  • $(document).ready(function() { Ensures the code executes after the document is fully loaded.
  • $("[name='fruit']").on("click", function() { Sets up a click event listener for the radio buttons with the name attribute set to “fruit”.
  • var selectedFruit = $("[name='fruit']:checked").val(); Retrieves the value of the clicked radio button that is currently checked (selected).
  • console.log("Selected fruit: " + selectedFruit); Logs the selected fruit value to the console.
$(document).ready(function() {
    $("[name='fruit']").on("click", function() {
        var selectedFruit = $("[name='fruit']:checked").val();
        console.log("Selected fruit using attribute name with value: " + selectedFruit);
    });
});
  • The attribute selector [name=”fruit”] targets all elements with the attribute name set to “fruit”.
  • The :checked selector narrows down the selection to the radio button that is currently checked.
  • The .val() method retrieves the value of the selected radio button.
Tip: If you’re working with more complex forms, consider using labels and CSS classes to make your code more organized and readable.

3. Getting Radio Selected Value Using Attribute Type and Value

In this method, you’ll use the [type=”radio”] attribute selector to target the radio buttons and :checked to select the checked one.

HTML:
Add the type=”radio” attribute to each radio button.

<input type="radio" name="fruit" class="fruit-radio" value="apple"> Apple <input type="radio" name="fruit" class="fruit-radio" value="banana"> Banana <input type="radio" name="fruit" class="fruit-radio" value="orange"> Orange

jQuery: 

$(document).ready(function() {
    $("[type='radio']").on("click", function() {
        var selectedFruit = $("[type='radio']:checked").val();
        console.log("Selected fruit using attribute type & value: " + selectedFruit);
    });
})

  • The attribute selector [type=”radio”] targets all radio button elements on the page.

  • The :checked selector filters the selection to the radio button that is currently checked.

  • The .val() method retrieves the value of the selected radio button.
In each of the three methods, the code listens for a click event on the radio buttons with the name “fruit.” When a radio button is pressed, the value of the selected fruit is obtained using the proper process. The output is logged to the console, indicating the method’s chosen fruit.
Using these three approaches, you may pick the way that best matches your coding style and project needs while still accomplishing the same purpose of getting the selected radio button value.

Caution: Ensure that your radio buttons have unique values within the same group to avoid confusion when retrieving values using jQuery.

Some Alternative Approches

  There are some alternative approaches for capturing the selected value of radio buttons using jQuery:

1. Using the .change() Event

The '.change()' event is triggered when the value of an input element changes, typically when a user interacts with it. It is specifically designed for capturing changes in input elements, making it an ideal choice for radio buttons.

$(document).ready(function() {
    $(".fruit-radio").on("change", function() {
        var selectedFruit = $(".fruit-radio:checked").val();
        console.log("Selected fruit using .change() event: " + selectedFruit);
    });});

2. Using the .click() Event

The .click() event is triggered when an element is clicked. It’s a common way to handle user interactions.

$(document).ready(function() {
    $(".fruit-radio").click(function() {
        var selectedFruit = $(".fruit-radio:checked").val();
        console.log("Selected fruit using class: " + selectedFruit);
    });
});

However, for radio buttons, using the .click() event might not be the most optimal choice, as it won’t cover all scenarios, such as keyboard navigation. 

In simple words, this article will explain to you how to use jQuery to understand and capture your radio button selections. Websites get smarter and more engaging as a result of this.

References

Here are some reference links related to jQuery: