• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Uncheck a Radio Button Using JavaScript

Uncheck a Radio Button Using JavaScript

August 13, 2022 Leave a Comment

We can uncheck a radio button using JavaScript by making use of the getElementById() method along with targeting the checked property value of an element. We simply need to change the checked property value of the radio button to false. Here is how we can do this:

document.getElementById("radioButton").checked = false;

Let’s see an example of this below.


Lets say we have the following HTML:

<div id="div1">
  <p>Select your favorite mode of transportation:</p>
  <form action="">
    <input type="radio" id="bike" name="transport" value="Bike"> <label for="bike">Bike</label><br>
    <input type="radio" id="car" name="transport" value="Car"> <label for="car">Car</label><br>
    <input type="radio" id="bus" name="transport" value="Bus"> <label for="bus">Bus</label><br>
    <input type="radio" id="train" name="transport" value="Train"> <label for="train">Train</label><br>
    <input type="radio" id="boat" name="transport" value="Boat"> <label for="boat">Boat</label><br>
    <input type="radio" id="airplane" name="transport" value="Airplane" checked> <label for="airplane">Airplane</label><br>
</div>
    <div id="click-me" onclick="uncheckRadio()">Remove selected radio button Airplane</div><br>
</div>
  </form>

In this example, we will use the onclick event to call a function uncheckRadio() which we will create below.

The uncheckRadio() function will simply uncheck the last radio button, Airplane.

Here is our function using JavaScript:

function uncheckRadio(){
  document.getElementById("airplane").checked = false;
}; 

Let’s try this out below:

Code Output:

Select your favorite mode of transportation:






Remove selected radio button Airplane

Full Code:

<div id="div1">
  <p>Select your favorite mode of transportation:</p>
  <form action="">
    <input type="radio" id="bike" name="transport" value="Bike"> <label for="bike">Bike</label><br>
    <input type="radio" id="car" name="transport" value="Car"> <label for="car">Car</label><br>
    <input type="radio" id="bus" name="transport" value="Bus"> <label for="bus">Bus</label><br>
    <input type="radio" id="train" name="transport" value="Train"> <label for="train">Train</label><br>
    <input type="radio" id="boat" name="transport" value="Boat"> <label for="boat">Boat</label><br>
    <input type="radio" id="airplane" name="transport" value="Airplane" checked> <label for="airplane">Airplane</label><br>
</div>
    <div id="click-me" onclick="uncheckRadio()">Remove selected radio button Airplane</div><br>
</div>
  </form>

<script>

function uncheckRadio(){
  document.getElementById("airplane").checked = false;
}; 

</script>

To check a button using JavaScript, we simply need to set the checked attribute to true.

Using JavaScript to Get the Value of a Radio Button

We can use JavaScript to get the value of a radio button using the querySelector() method.

document.querySelector('input[name="radio1"]:checked').value;

Lets say we have the following HTML:

<div id="div2">
  <p>Select your favorite mode of transportation:</p>
  <input type="radio" id="bike1" name="transport" value="Bike"><label for="bike1">Bike</label><br>
  <input type="radio" id="car1" name="transport" value="Car"><label for="car1">Car</label><br>
  <input type="radio" id="bus1" name="transport" value="Bus"><label for="bus1">Bus</label><br>
  <input type="radio" id="train1" name="transport" value="Train" checked><label for="train1">Train</label><br>
  <input type="radio" id="boat1" name="transport" value="Boat"><label for="boat1">Boat</label><br>
  <input type="radio" id="airplane1" name="transport" value="Airplane"><label for="airplane1">Airplane</label><br>
</div>

In this example, the checked radio button to start will be the Train option. We can use our code above to get this value using JavaScript.

Here is the jQuery code we will need:

var selectedRadioButton = document.querySelector('#div2 input[name="transport"]:checked').value;

Try it out below by selecting a radio button and then clicking the button below to see what radio button is checked:

Code Output:

Select your favorite mode of transportation:






Check which radio button is selected with JavaScript

Full Code:

<div id="div2">
  <p>Select your favorite mode of transportation:</p>
  <input type="radio" id="bike1" name="transport" value="Bike"> <label for="bike1">Bike</label><br>
  <input type="radio" id="car1" name="transport" value="Car"> <label for="car1">Car</label><br>
  <input type="radio" id="bus1" name="transport" value="Bus"> <label for="bus1">Bus</label><br>
  <input type="radio" id="train1" name="transport" value="Train" checked> <label for="train1">Train</label><br>
  <input type="radio" id="boa1t" name="transport" value="Boat"> <label for="boat1">Boat</label><br>
  <input type="radio" id="airplane1" name="transport" value="Airplane"> <label for="airplane1">Airplane</label><br>
</div>
  <div id="click-me" onclick="selectedRadio()">Check which radio button is selected with JavaScript</div>
  <div id="results"></div>
</div>

<script>

function selectedRadio(){
  var selectedRadioButton = document.querySelector('#div2 input[name="transport"]:checked').value;
  document.getElementById("results").textContent = selectedRadioButton;
}; 

</script>

Hopefully this article has been useful to help you understand how to uncheck a radio button in JavaScript.

Other Articles You'll Also Like:

  • 1.  Using JavaScript to Remove Substring From String
  • 2.  Subtract All Numbers in an Array Using JavaScript
  • 3.  Changing the Background Image of a div in JavaScript
  • 4.  Using JavaScript to Compare Dates
  • 5.  Using JavaScript to Change the href of a Link
  • 6.  Using JavaScript to Check if String Contains Numbers
  • 7.  Using JavaScript to Rotate an Image
  • 8.  Using JavaScript to Square a Number
  • 9.  Using JavaScript to Get the Decimal Part of a Number
  • 10.  Using JavaScript to Return Two Values

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy