ocultar un elemento html cuando un button radio deja de estar marcado in javascript

To hide an HTML element when a radio button is not checked, you can add an event listener to the radio buttons and use JavaScript to check if the button is checked or not. If it is not checked, you can use CSS to set the display property of the element to "none".

Here's an example code snippet that hides an HTML element with the ID "myElement" when a radio button with the ID "myRadioButton" is unchecked:

<input type="radio" id="myRadioButton" name="myRadioGroup"> My Radio Button
<div id="myElement">This element will be hidden when the radio button is unchecked</div>

<script>
  var radio = document.getElementById("myRadioButton");
  var element = document.getElementById("myElement");

  radio.addEventListener("change", function() {
    if (radio.checked) {
      element.style.display = "block";
    } else {
      element.style.display = "none";
    }
  });
</script>
471 chars
16 lines

In this example, we first get references to the radio button and the HTML element using their IDs. We then add an event listener to the radio button that listens for changes. When the radio button changes, we check if it is checked or not using its checked property. If it is checked, we set the display property of the element to "block", which shows it. If it is not checked, we set the display property to "none", which hides it.

Note that the radio button must have a unique ID and a name attribute in order to work properly with other radio buttons in the same group.

gistlibby LogSnag