input radio does not hide content when unchecked


input radio does not hide content when unchecked, i can’t make the content be hidden when the radio input is unchecked

how can I hide the content of the unmarked radio input?
clicking on another radio input is unchecked but does not hide the content

$('#alternar').click(function () {

$('#alternarx').click(function () {

/* commented out because this select doesn't appear in the HTML:
  placeholder: "Make a Selection",
  allowClear: true

function uncheckAndCheck(event) {

    // gets all radios with the name prefix like 'custom-radio-'
    // and uncheck all of them
    document.querySelectorAll("input[type='radio'][name^='custom-radio-']").forEach(radio => {
        radio.checked = false;

    // checks the radio that triggered the click event = true;

<script src=""></script>

         <input type="radio" class="new-control-input" name="custom-radio-1" id="alternarx" onclick="uncheckAndCheck(event)"/>
        <div id="prueba"> Content1 </div>
        <input type="radio" class="new-control-input" name="custom-radio-2" id="alternar" onclick="uncheckAndCheck(event)"/>
        <div id="pruebax"> Content2 </div>


George’s solution works, but is reliant upon the HTML never changing. If you add any element between the checkbox and the div, it will break the functionality.

To answer your question related to JavaScript:

It’s unnecessary to check and uncheck the other radio inputs. You just need to give them the same name attribute.

Second, you’re .toggle()ing the divs on click. That might be why they’re acting strangely. You’re not checking if the radio button is selected or not, and that’s going to result in them toggling even when you click them when they’re already selected. Luckily, you can just listen for them to change states.

Third, you can hold a selector for the target of the radio button you want to show/hide in a data attribute, and use one function for all of this.

Fourth, why mix inline onclick attributes, when you’re using jQuery? Just listen for the event using the built-in listeners in jQuery.

//jQuery shorthand for $(document).ready(function(){ to be sure your DOM has loaded:
$(function() {

    //run this on page load, too. Necessary because browsers will remember which one is checked on a page *refresh*, and hides the target divs initially when nothing is checked:
    $checkedRB = $(".rbToggleDiv:checked");
    if($checkedRB.length > 0) {
    } else {
    //both radio buttons have the same class as well, so you can listen for either of them to change states:
    $(document).on("change", ".rbToggleDiv", function(e) {
      //this = radio button that has changed
      var $thisRB = $(this); //turn it into a jQuery object
      if($thisRB.prop("checked")) { //only do something if this RB is checked
    function toggleVisibleDivs($targetRB) {
      if ($targetRB === false) { //no target sent in
        //hide all 
        $(".pruebaDiv").hide(); //hide all divs
      } else { //target sent in
        if ($"target-div")) { //make sure the data is set
          var targetSelector = $"target-div"), //grab the string from the data object
              $targetDiv = $(targetSelector); //use it to select the target div
              if ($targetDiv.length > 0) { //make sure the div is selected
                //hide all divs with the same class:
                //then, show only the one you want visible, the $targetDiv:
              } else {
                console.error("Div not found!", targetSelector);
        } else {
          //data not set:
          console.error("Data was not set.");
.pruebaDiv {
    display: none;
<script src=""></script>
<!-- if they have the same names, they will act as a radio button list, and will act accordingly. Also, you should really choose more descriptive IDs and names: -->

<input type="radio" class="rbToggleDiv" name="rb-toggle-div" id="alternarx" data-target-div="#prueba" />
<input type="radio" class="rbToggleDiv" name="rb-toggle-div" id="alternar" data-target-div="#pruebax" />

<!-- for the sanity of the user, I've moved these two divs next to each other below the radio buttons so they don't move around: -->
<div class="pruebaDiv" id="prueba"> Content1 </div>
<div class="pruebaDiv" id="pruebax"> Content2 </div>

Answered By – Sean Kendle

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published