JavaScript

Different Ways to Get Value from Radio Button in JavaScript

In JavaScript, we usually come across web pages where it is required to get the radio button value to verify if the entered data is correct or not. For instance, you need to submit a form and notify the user if the data entered is relevant. In such cases, getting value from the radio button in JavaScript is a very helpful feature for saving the end-user time and ensuring data security.

Different Ways to Get Value from Radio Button in JavaScript

To get value from the radio button in JavaScript, the following methods can be utilized:

We will now go through each of the listed methodologies one by one!

Method 1: Get Value from Radio Button in JavaScript Using getElementById() and getElementsByName() Methods

The “getElementById()” method fetches an element with a specified id and the “getElementsByName()” method accesses the elements with the name specified in its argument. These methods can be used to get both the attributes’ button id and the name value for accessing them and displaying their values.

Syntax

document.getElementById(elementID)

Here, “elementID” is referring to the specified id of the added element.

document.getElementsByName(name)

In the above syntax, “name” is the element’s name value.

The following example will explain the stated concept clearly.

Example

In the following example, we will include a heading in the “<h>” tag with a line break as follows:

<h>Select any one:</h>

<br>

Now, assign the input type as “radio” the radio button, and specify its id, name, and value:

<input type= "radio" id= "email" name= "contact" value= "email">

Also, include a “label” for the input type with a value named “Email”:

<label for= "email">Email</label>

Similarly, repeat the discussed steps for the “Phone No.” field:

<input type= "radio" id= "contact" name= "contact" value= "Contact Number">

<label for="contact">Phone No.</label>

<br>

After that, include a button named “Submit” for getting the value upon clicking:

<button id= "submit">Submit</button>

Now, fetch the created button using the “document.getElementById()” method in the JavaScript file. Also, apply an “onclick” event to access a function. In the function defined below, access both the input types using the “document.getElementsByName()” method as the “name” attribute in both the input types is the same.

Finally, apply a “for” loop and get the value of the selected radio button using the “checked” property and display it in the alert box:

document.getElementById('submit').onclick= function(){
var value= document.getElementsByName('contact');
for (var radio of value){
if (radio.checked) {    
            alert(radio.value);
                    }
 }}

The output of the above program will result as follows:

Method 2: Get Value from Radio Button in JavaScript Using the find() Method

The “find()” method accesses the value of the first element. This method can be implemented to get the value of the checked radio button by finding the specific attributes with the help of the added name.

Syntax

Array.from(value).find(radio => radio.checked)

Here, “from()” method will access the specified element, “find()” method will match it with the value of the checked radio button, and the resultant value will be returned.

Look at the following example.

Example

For HTML, we will use the same code given in the previous method. In our JavaScript file, we will fetch the button id using the “document.getElementById()” method with an “onclick” event accessing the function, which will firstly get both the input types having the same name attribute “contact” using the “document.getElementsByName()” method.

Lastly, apply the “from()” method to access the value stored in the variable named “value”, and the “find()” method will get the checked state of the input. Finally, the “value” property will return the value of the specific radio button marked as checked:

document.getElementById('submit').onclick= function() {
var value= document.getElementsByName("contact");      
var selectValue= Array.from(value).find(radio => radio.checked);
    alert(selectValue.value);
}

Output

Method 3: Get Value from Radio Button in JavaScript Using the checked() property

The “checked” property returns the checked state of the specified input type. This method can be applied to check the checked condition for each of the radio buttons and return the corresponding value.

Syntax

checkboxObject.checked

In the given syntax, “checkboxObject” refers to the specified object to be checked.

Example

First, add a “label” using “<label for>” tag for specifying a particular category followed by a line break in “<br>” tag:

<label for="gender">Select any one: </label>

<br>

Next, repeat the above discussed procedures for specifying two input types as “radio” and assign the given values followed by a line break shown below:

<input type= "radio" id= "gender" name= "gender" value= "Male" checked= "Male">Male</input>

<input type= "radio" id= "genderFem" name= "gender" value= "female" checked= "Female">Female</input>

<br>

Also, include an input type named “submit” to get the radio button value upon clicking. An “onclick” event will also be added to access the specified function upon clicking submit:

<input type="submit" value="submit" onclick="submitData()">

After that, declare a function named “submitData()” and get the elements of the specified ids and store them in the variables named “get” and “get1”. Finally, apply a condition that if the radio button referring to the “Male” or “Female” gender is accessed, the “checked” property will check it, and the “value” property will fetch the corresponding value against each of the checked radio button:

functionsubmitData(){
get= document.getElementById("gender")
    get1= document.getElementById("genderFem")
if (get.checked == true){  
    alert(document.getElementById("gender").value);  
  }
elseif (get1.checked == true){  
    alert(document.getElementById("genderFem").value);  
  }

Output

We have compiled different ways to get value from a radio button in JavaScript.

Conclusion

To get value from the radio button in JavaScript, apply the “getElementById()” and “getElementsByName()” methods for accessing both the attributes at the same time and displaying their corresponding values, or “find()” method for getting the radio buttons checked by finding the specified attributes based on their set names or the “checked” property method to apply a condition for each of the attributes and get their value. This blog is guided related to the procedure of getting value from the radio button in JavaScript.

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.