×

JavaScript radio button checked value

What are radio buttons?

A radio button is defined as an icon that is used to take input from the user. The user can choose only one option(value) from the group of radio buttons. Radio buttons are used for the single selection from multiple options in the group.

The radio group must have a common name. However, the value attribute is used to define the unique value associated with each radio button. 

The radio buttons are usually hollow circles.

The <input> tag is used to define a radio button.

Syntax:

<input type=”radio”>

Example:

<!DOCTYPE html>
<html>
  
<head>
    <title>
         Javascript radio buttons
    </title>
</head>


<body>


<form>
     Which fruit do you like?<br>
    <input type="radio" name="fruit" value="Apple" id = "Apple">Apple<br>
    <input type="radio" name="fruit" value="Orange" id = "Orange" >Orange<br>
    <input type="radio" name="fruit" value="Banana" id = "Banana" >Banana<br>
    <input type="radio" name="fruit" value="Kiwi" id = "Kiwi">Kiwi<br>
    <input type="radio" name="fruit" value="Mango" id = "Mango">Mango<br>


</form>
<br>


<button onclick="submit()">Submit</button>






</body>
</html>

Output:

JavaScript Radio Button Checked Value

What is a checked value?

The value of the selected(marked) radio button in the whole group is known as the checked value.

In the above example, “orange” is the checked value.

Checked property in JavaScript

The checked property either sets or returns the status of radio buttons, i.e., helps to determine whether it is checked or not.

This property is supported by mostly all the browsers like Google chrome, safari, Microsoft Edge/Internet Explorer, Oracle, Firefox etc.

The following are the two ways the “checked” property can be used:

How to check the value in a radio button using JavaScript?

The checked property can be used to check or uncheck the value of any radio button.

NOTE: It is important to define an “id” for each radio button, to uniquely identify the buttons.

Syntax:

radioObject.checked = true or false

Example:

<!DOCTYPE html>
<html>
  
<head>
    <title>
         Javascript checked value of radio button
    </title>
</head>


<body>


<form>
     Which fruit do you like?
    <input type="radio" name="fruit" value="Apple" id = "Apple">Apple
    <input type="radio" name="fruit" value="Orange" id = "Orange" >Orange
    <input type="radio" name="fruit" value="Banana" id = "Banana" >Banana
    <input type="radio" name="fruit" value="Kiwi" id = "Kiwi">Kiwi
    <input type="radio" name="fruit" value="Mango" id = "Mango">Mango


</form>
<br>
<button onclick="checkb()">Check "Banana"</button>
<button onclick="uncheckb()">Uncheck "Banana"</button>
<br><br>
<button onclick="checkm()">Check "Mango"</button>
<button onclick="uncheckm()">Uncheck "Mango"</button>


<script>
function checkb() {
  document.getElementById("Banana").checked = true;
}
function uncheckb() {
  document.getElementById("Banana").checked = false;
}
function checkm() {
  document.getElementById("Mango").checked = true;
}
function uncheckm() {
  document.getElementById("Mango").checked = false;
}
</script>


</body>
</html>

Output:

If “Check Mango” is selected,

JavaScript Radio Button Checked Value

If “Uncheck Mango” is selected,

JavaScript Radio Button Checked Value

NOTE: Even though the above example has two separate buttons to “check” the different radio buttons, but only one radio button can be selected at a time. If both the buttons are pressed, the button that was executed in the last, executes its function, and the other radio button gets unchecked automatically.

How to return the value of a checked radio button using JavaScript?

A user-defined function needs to be defined to return the value of the checked radio button. The function refers to all the radio buttons through name attributes. It uses the “checked” property to find the value of the selected radio button. 

The checked property returns a Boolean value: True or False.

  • “true” indicates that the radio button is selected.
  • “false” indicates that the radio button is not selected.

NOTE: In the case of web pages with multiple radio buttons, initially, all the user-defined function fetches all the <input> tags, then the <input> tags with type as "radio" are selected. These radio buttons are then selected and displayed.

Syntax:

radioObject.checked

Example 1:

Here the example shows the web page which contains a single checked Radio button, i.e. when only a single list is present,

<!DOCTYPE html>
<html>
  
<head>
    <title>
         Javascript checked value of radio button
    </title>
</head>
  
<body>
    <p>
        Select a radio button from the list given below and click on the button.
    </p>
      
    Which fruit do you like?
    <input type="radio" name="fruit" value="Apple">Apple
    <input type="radio" name="fruit" value="Orange">Orange
    <input type="radio" name="fruit" value="Banana">Banana
    <input type="radio" name="fruit" value="Kiwi">Kiwi
    <input type="radio" name="fruit" value="Mango">Mango
      
    <br>
      
    <button type="button" onclick="displayCheckedRadioValue()">
        Done
    </button>
      
    <br>
      
    <div id="content"></div>
      
    <script>
        function displayCheckedRadioValue() {
            var ele = document.getElementsByName('fruit');
              
            for(i = 0; i < ele.length; i++) {
                if(ele[i].checked)
                document.getElementById("content").innerHTML
                        = "Your preferred fruit is "+ele[i].value+ ".";
            }
        }
    </script>
</body>
</html> 

Output:

JavaScript Radio Button Checked Value

Example 2:

Here the example shows the web page which contains multiple checked Radio buttons(values of different lists),

<!DOCTYPE html>
<html>
  
<head>
    <title>
                 Javascript checked value of radio button
    </title>
</head>
  
<body>
    <p>
        Select a radio button from the list given below and click on the button.
    </p>
      
    Which fruit do you like?
    <input type="radio" name="fruit" value="Apple">Apple
    <input type="radio" name="fruit" value="Orange">Orange
    <input type="radio" name="fruit" value="Banana">Banana
    <input type="radio" name="fruit" value="Kiwi">Kiwi
    <input type="radio" name="fruit" value="Mango">Mango
    <br>
      
        Which vegetable do you like?
    <input type="radio" name="vegetable" value="Onion">Onion
    <input type="radio" name="vegetable" value="Capsicum">Capsicum
    <input type="radio" name="vegetable" value="Carrot">Carrot
    <input type="radio" name="vegetable" value="Potato">Potato
    <input type="radio" name="vegetable" value="Spinach">Spinach
      
    <br>
      
    <button type="button" onclick="displaycheckedRadioValue()">
        Done
    </button>
      
    <br>
      
    <div id="content"></div>
      
    <script>
        function displaycheckedRadioValue() {
            document.getElementById("content").innerHTML = "";
            var ele = document.getElementsByTagName('input');
              
            for(i = 0; i < ele.length; i++) {
                  
                if(ele[i].type="radio") {
                  
                    if(ele[i].checked)
                        document.getElementById("content").innerHTML
                                += ele[i].name + " preference is "
                                + ele[i].value + "."  + "<br>";
                }
            }
        }
    </script>
</body>
  
</html>

Output:

JavaScript Radio Button Checked Value

Related Topics

JavaScript Console log Multiple variables

What does console.log() do in JavaScript? The console.log() is a function in JavaScript language. It is used to: Print any message in the console which is visible to the userPrint the values...

3 minutes read.

Javascript input maxlength

Various attributes often accompany the input tag in HTML to change the properties of the input field. Maxlength() is one such attribute. The maxLength attribute defines the maximum number of characters...

2 minutes read.

Javascript Object

An object is an entity having state and behavior. JavaScript is an object oriented scripting language. JavaScript is template based not class based but we can create object directly. Syntax to...

2 minutes read.

JavaScript Arrays

JavaScript Arrays: JavaScript Arrays are a type of variables that allows us to store the individual or the group of values under a single variable. What is an Array in JavaScript? The array...

6 minutes read.

Synchronous and Asynchronous in JavaScript

JavaScript is a powerful and versatile language that is used all over the web, from small websites to large applications. It is essential for developers to understand the differences between...

9 minutes read.

JavaScript redirect URL with parameters

Data can be often passed between web pages as information in URL parameters or query strings. A URL (Uniform Resource Locator) can have a single parameter or multiple parameters.Parameters can...

4 minutes read.

Javascript Password Validation

Here we see that how to validate any password field. Like password field can be blank and length of password is minimum 8 characters. Example <!DOCTYPE html>   <html>   <head>   <script>   functionpass_validation()   {     var password=document.myform.password.value;     if (password==null || password=="")   {     alert("password can't be blank");     return false;     }   else if(password.length<8)   {     alert("Password must be at least 8 characters long.");     return false;       }     }     </script>   </head>   <body>   <form name="myform" method="post" action="register.php" onsubmit="return pass_validation()" >   Password: <input type="password" name="password">   <input type="submit" value="submit">   </form>   </body>   </html> Try Now ← Prev Next → ...

1 minute read.

Javascript Dialog Box

A JavaScript dialog box is predefined function which is used to perform different task. Some functions are used in JavaScript Dialog box. FunctionDescriptionalert()To give alert message to userprompt()To input value from...

2 minutes read.

D3.js Tutorial

Introduction of D3.js D3.js is referred to as a JavaScript library that is used to manipulate the documents according to the data. The intensity of D3 is based on web standards...

5 minutes read.

JavaScript setTimeout()

The setTimeout() method creates a timer that, when it expires, runs a function or provides a piece of code. setTimeout() is an asynchronous function, which means that it will not interrupt...

3 minutes read.

Javascript Example

What is JavaScript? JavaScript is a highly versatile programming language. It provides the capability to add interactivity to static web pages, which are designed with HTML and CSS. It also provides...

5 minutes read.

Javascript Math

Math is build-in object that has properties and methods for mathematical constants and functions. It allows you to perform mathematical tasks on numbers. Syntax varpi_val = Math.PI; varsin_val = Math.sin(30); Examples Math.pow() Math.pow(x,y) returns the value of x to the power...

2 minutes read.

Array to String in JavaScript

As a web developer, you often have to deal with multiple data types in a single application. How to convert an array to a string in JavaScript is essential for...

19 minutes read.

Javascript Forms Validation

Form validation works at server, after client had entered all the necessary details and then pressed submit button. JavaScript provides the facility to validate the form on the client side...

9 minutes read.

JavaScript Image resize before upload

Image resizing can be a quite tedious task, so it is usually done on the server-side. The resized image file is then delivered to the client side. However, sometimes the...

3 minutes read.

Javascript Email Validation

We can validate the email with the help of JavaScript. Here we check the condition related to any email id, like email it must have "@" and "." sign and...

2 minutes read.

Javascript Redirect Button

What is a redirect button? When a button acts as a hyperlink, it is known as a redirect button. On clicking the button, it transfers the user to another page. How to...

3 minutes read.

What is Ternary Operator in JavaScript?

In some cases, a ternary operator can be used instead of an if-else expression. A ternary operator examines a condition and then runs a block of code in response to...

4 minutes read.

Javascript Tutorial

JavaScript is a light-weight, dynamic scripting language that is mainly employed to create interactive front-end web applications. It was originally developed in 1995 by Brendan Eich at Netscape Communications Corporation...

5 minutes read.

Callback and Callback Hell in JavaScript

The widely used client-side programming language JavaScript is simple, light, and interpretive. JavaScript is used by the majority of client-side web applications. It is also possible to use JavaScript on...

3 minutes read.