javascript - How to unselect a radio button and re-select the default button on click?

Javascript - How to unselect a radio button and re-select the default button on click?

To unselect a radio button and then re-select the default button on click, you need to handle the click event of the radio button and programmatically set its checked state accordingly. Here's how you can achieve this:

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Unselect and Re-select Radio Button</title> </head> <body> <label> <input type="radio" name="options" value="option1" id="option1" checked> Option 1 </label> <label> <input type="radio" name="options" value="option2" id="option2"> Option 2 </label> <label> <input type="radio" name="options" value="option3" id="option3"> Option 3 </label> <script> // Get the radio buttons const option1 = document.getElementById('option1'); const option2 = document.getElementById('option2'); const option3 = document.getElementById('option3'); // Add click event listener to each radio button option1.addEventListener('click', handleRadioButtonClick); option2.addEventListener('click', handleRadioButtonClick); option3.addEventListener('click', handleRadioButtonClick); function handleRadioButtonClick(event) { const radioButton = event.target; // If the clicked radio button is already checked, uncheck it if (radioButton.checked) { radioButton.checked = false; // Re-select the default radio button (Option 1) option1.checked = true; } } </script> </body> </html> 

In this example:

  • We have three radio buttons with ids option1, option2, and option3.
  • We attach a click event listener to each radio button.
  • When a radio button is clicked, we check if it's already checked. If it is, we uncheck it and then re-select the default radio button (Option 1) by setting its checked property to true.

This way, clicking on a checked radio button will unselect it and re-select the default radio button.

Examples

  1. JavaScript Unselect Radio Button: Learn how to programmatically unselect a radio button in JavaScript.

    // Unselecting a radio button by setting checked to false document.querySelector('input[name="myRadio"]').checked = false; 
  2. JavaScript Reset Radio Button: Resetting a radio button to its default selection on click.

    // Resetting to default selection on click event const defaultRadioButton = document.querySelector('input[name="defaultRadio"]'); document.querySelectorAll('input[name="myRadio"]').forEach(radioButton => { radioButton.addEventListener('click', () => { defaultRadioButton.checked = true; }); }); 
  3. JavaScript Re-select Default Radio Button: Re-selecting the default radio button after clicking on another option.

    // Re-selecting default option after click event const defaultRadioButton = document.querySelector('input[name="defaultRadio"]'); document.querySelectorAll('input[name="myRadio"]').forEach(radioButton => { radioButton.addEventListener('click', () => { defaultRadioButton.checked = true; }); }); 
  4. JavaScript Reset Radio Group: Resetting a radio button group to its default selection.

    // Resetting radio button group to default selection const defaultRadioButton = document.querySelector('input[name="defaultRadio"]'); defaultRadioButton.checked = true; 
  5. JavaScript Uncheck Radio Button: Unchecking a radio button within a group.

    // Unchecking a specific radio button within a group document.getElementById('radioButtonId').checked = false; 
  6. JavaScript Radio Button Reset Event: Handling the reset event to revert to the default radio button selection.

    // Handling reset event to re-select default option document.getElementById('radioForm').addEventListener('reset', () => { const defaultRadioButton = document.querySelector('input[name="defaultRadio"]'); defaultRadioButton.checked = true; }); 
  7. JavaScript Radio Button Click Event: Implementing a click event listener to reset the radio button selection.

    // Click event listener to reset radio button selection document.getElementById('radioButtonId').addEventListener('click', () => { const defaultRadioButton = document.querySelector('input[name="defaultRadio"]'); defaultRadioButton.checked = true; }); 
  8. JavaScript Radio Button Default Selection: Ensuring a default radio button is always selected after interaction.

    // Ensuring default selection after interaction const defaultRadioButton = document.querySelector('input[name="defaultRadio"]'); document.querySelectorAll('input[name="myRadio"]').forEach(radioButton => { radioButton.addEventListener('change', () => { if (!radioButton.checked) { defaultRadioButton.checked = true; } }); }); 
  9. JavaScript Reset Radio Button Group: Resetting the entire radio button group to its default state.

    // Resetting radio button group to default state const defaultRadioButton = document.querySelector('input[name="defaultRadio"]'); document.querySelectorAll('input[name="myRadio"]').forEach(radioButton => { radioButton.addEventListener('click', () => { defaultRadioButton.checked = true; }); }); 
  10. JavaScript Radio Button Default Selection on Form Reset: Implementing default radio button selection when the form is reset.

    // Default selection on form reset document.getElementById('radioForm').addEventListener('reset', () => { const defaultRadioButton = document.querySelector('input[name="defaultRadio"]'); defaultRadioButton.checked = true; }); 

More Tags

uibutton center sha sql-server-2008 snakeyaml jhipster displayattribute cancellationtokensource mass-assignment netcdf

More Programming Questions

More Various Measurements Units Calculators

More Bio laboratory Calculators

More Internet Calculators

More Electronics Circuits Calculators