Home > Web Front-end > JS Tutorial > body text

How to uncheck a radio button using JavaScript/jQuery?

WBOY
Release: 2023-08-31 10:25:02
forward
790 people have browsed it

How to uncheck a radio button using JavaScript/jQuery?

Sometimes, we need to uncheck a radio button using JavaScript or JQuery. For example, we use radio buttons in a form. When the user presses the clear form button, we need to uncheck all the radio buttons and again allow the user to select any option from the radio buttons.

In this tutorial, we will learn various ways to uncheck single or multiple radio buttons using JQuery or JavaScript.

Use JavaScript to uncheck a radio button

We can access radio buttons in JavaScript using id, tag, name or other means. Afterwards, we can uncheck the checked property of the radio button by assigning a false boolean value.

grammar

Users can use the checked attribute to uncheck the radio button according to the syntax below.

radio.checked = false; 
Copy after login

In the above syntax, radio is the radio button accessed using JavaScript.

Example 1

In the example below, we create radio buttons. The user can check the radio button by clicking on it. Later, when the user clicks the button, it calls the clearRadio() function.

In the clearRadio() function, we use the id of the radio input to access it. Next, we access the selected property of the radio input and assign a false boolean value to uncheck the radio button.

<html>
<body>
   <h2> Using the <i> Checked property of radio button </i> to uncheck radio button in JavaScript. </h2>
   <br>
   <label for = "radio_btn"> Test </label>
   <input type = "radio" id = "radio_btn" value =   "test">
   <br> <br>
   <button onclick = "clearRadio()"> Uncheck Radio </button>
   <br> <br>
   <div id = "output"></div>
</body>
<script>
   let output = document.getElementById("output");
   function clearRadio() {
      let radio = document.getElementById("radio_btn");
      radio.checked = false;
   }
</script>
</html>
Copy after login

In the above output, the user can observe that the radio input is cleared whenever the "Uncheck Radio" button is pressed.

Uncheck radio button using JQuery

We can also use JQuery to clear the radio input. In JQuery, we can set any attribute for an HTML element using the prop() method. The prop() method takes two values ​​as parameters: the property name and the property value.

Here we will pass "checked" as the property name and false as the property value to uncheck a specific radio button using JQuery.

grammar

Users can use the following syntax to uncheck a radio button using JQuery's prop() method.

$("#radio_btn").prop('checked', false); 
Copy after login

In the above syntax, we pass the "checked" attribute name as the first parameter and the false attribute value as the second parameter.

Example 2

In the example below, we create a radio button. When a click event is fired on the button, it will clear the radio button by setting a false value for the selected property using the prop() method.

<html>
<head>
   <script src ="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.3/jquery.min.js"></script>
</head>
<body>
   <h2> Using the <i> prop() method </i> to uncheck radio button in JQuery</h2>
   <label for = "radio_btn"> Clear </label>
   <input type = "radio" id = "radio_btn" value = "clear">
   <br> <br>
   <button id = "btn"> Uncheck Radio </button>
   <br> <br>
   <div id = "output"></div>
</body>
<script>
   let output = document.getElementById("output");
      
      // detect button click
      $('#btn').click(function () {
      
         // Clear radio button
         $("#radio_btn").prop('checked', false);
      })
</script>
</html>
Copy after login

Example 3

We have created multiple radio inputs and grouped them in the example below. Therefore, the user can select any one from all four radio inputs. When the user clicks the button, it clears all radio input.

Here we access the radio input by type and name. So even if the user selects any radio input from the group, it will uncheck it.

<html>
<head>
   <script src ="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.3/jquery.min.js"></script>
</head>
<body>
   <h2> Using <i> jQuery </i> to clear multiple radio buttons </h2>
   <label for = "color"> Blue </label>
   <input type = "radio" id = "color" name = "color" value = "Blue">
   <label for = "color"> Black </label>
   <input type = "radio" id = "color" name = "color" value = "Black">
   <label for = "color"> Brown </label>
   <input type = "radio" id = "color" name = "color" value = "Brown">
   <label for = "color"> Green </label>
   <input type = "radio" id = "color" name = "color" value = "Green">
   <br> <br>
   <button id = "btn"> Clear all radio buttons </button>
   <br> <br>
   <div id = "output"></div>
</body>
<script>
   let output = document.getElementById("output");
   $('#btn').click(function () {
      
      // clear all radio buttons
      $("input[type=radio][name=color]").prop('checked', false);
      output.innerHTML = "All radio buttons are unchecked!"
   })
</script>
</html>
Copy after login

We learned to use JavaScript and jQuery to clear radio input when needed. Also, it is necessary to clear the radio input when the user submits the form. Users can access radio button groups by their type and name, and then clear all radio buttons for a single group.

The above is the detailed content of How to uncheck a radio button using JavaScript/jQuery?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!