JavaScript

[3 Ways] How to get the value of selected radio button by jQuery?

Share your learning

Today, I am going to answer this question to get the value of selected radio button by jQuery in 3 ways.

You might only need to use one of them as per your requirement. But I’ll cover the all 3 ways.

  1. When you need to get the value of selected radio button as just onchange event.
  2. You can also achieve this on any button click.
  3. And maybe you want to get the selected radio button value when going to submit the form.

Also Read: Single function with JavaScript to test the variable empty or not where variable might be array, string, number, undefined, null etc.

Example 1

<div class="container">
        
        <form class="mt-5" id="saveForm">
            <h4>User Role</h4>
            <div class="form-check">
              <input onchange="userRole(this)" type="radio" name="user_role" value="editor" class="form-check-input" id="editor">
              <label class="form-check-label" for="editor"> Editor </label>
            </div>
            <div class="form-check">
              <input onchange="userRole(this)" type="radio" name="user_role" value="maintainer" class="form-check-input" id="maintainer">
              <label class="form-check-label" for="maintainer"> Maintainer </label>
            </div>
            <div class="form-check">
              <input onchange="userRole(this)" type="radio" name="user_role" value="developer" class="form-check-input" id="developer">
              <label class="form-check-label" for="developer"> Developer </label>
            </div>

            <button type="button" class="btn btn-primary">Submit</button>
        </form>

    </div>
        function userRole(element) {
            console.log('object :>> ', element.value);
        }

Or can be used by jQuery change event.

      $(document).ready(function() {   
            $("[type='radio']").change(function(){
                console.log(this.value);
            })
        })

Example 2

      $(document).ready(function() {   
            $("button").click(function(){
                console.log('on button click :>> ', $("[type='radio']:checked").val());
            })
        })

Example 3

$(document).ready(function() {   
          $("#saveForm").on("submit", function(e){
                e.preventDefault();
                console.log($("[type='radio']:checked").val());
            })
})

That’s it.

Hope it will help.

If you have another way to get the value of selected radio button by jQuery, please share in the comment section below.

I’ll recommend w3school to learn about jQuery.

Satpal

Recent Posts

How to Switch PHP Versions in XAMPP Easily: Managing Multiple PHP Versions on Ubuntu

Today we are going to learn about managing multiple PHP versions on ubuntu with xampp.…

1 year ago

How to Use Coding to Improve Your Website’s SEO Ranking?

Let's understand about how to use coding to improve your website's SEO. In today’s computerized…

1 year ago

Most Important Linux Commands for Web Developers

Let's understand the most important linux commands for web developers. Linux, as an open-source and…

1 year ago

Top 75+ Laravel Interview Questions Asked by Top MNCs

Today we are going to discuss top 75+ Laravel interview questions asked by top MNCs.Laravel,…

1 year ago

Mailtrap Integration for Email Testing with Laravel 10

Today we will discuss about the Mailtrap integration with laravel 10 .Sending and receiving emails…

1 year ago

Firebase Cloud Messaging (FCM) with Ionic 6: Push Notifications

Today we are going to integrate FCM (Firebase Cloud Messaging) push notifications with ionic application.Firebase…

1 year ago