Make radio button disappear when clicked using JavaScript

My goal is to conceal radio button values upon clicking them, and while I have achieved this in a static context, I am seeking a dynamic solution. Here is an example of my current static implementation:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
 <title>Untitled</title>
<script language="JavaScript">
<!--  
function showDiv( id ) { 
    document.all.textBox01.style.visibility = 'hidden'; 
    document.all.textBox02.style.visibility = 'hidden'; 
    document.all.textBox01.value = ''; 
    document.all.textBox02.value = ''; 
    document.all[ id ].style.visibility = 'visible'; 
    document.all[ id ].focus(); 
}
    -->
</script> 
</head>

<body>

Option 1: 
<input type=radio name=radioBtn onClick="showDiv( 'textBox01' );"> 
<div id=textBox01 style="visibility:hidden">test2</div> 


Option 2: 
<input type=radio name=radioBtn onClick="showDiv( 'textBox02' );"> 
<div id=textBox02 style="visibility:hidden">test</div> 




</body>
</html>

I am looking for ways to achieve this dynamically and have attempted the following code snippet. Any feedback or suggestions would be greatly appreciated:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
 <title>Untitled</title>
<script language="JavaScript">
<!--  
function change(rad)
 {

  for(var i = 1; i <= 5; ++i)
  {
   if(i <= rad.value)
   { document.getElementById("radio" + i).style.visibility = "hidden";
   }else
   { document.getElementById("radio" + i).style.value = "";
   }
  }
 }

    -->
</script> 
</head>

<body>

Option 1: 
<input type=radio name=radioBtn onClick="change(this)"> 
<div id=radio1 style="visibility:hidden">test2</div> 


Option 2: 
<input type=radio name=radioBtn onClick="change(this)"> 
<div id=radio2 style="visibility:hidden">test</div> 




</body>
</html>

Answer №1

Implement this JavaScript function to show/hide radio buttons based on user input:

function customizeRadio(rad) {
  var element;
  
  for(var i = 1; i <= 5; ++i) {
    element = document.getElementById("radio" + i);
    
    if(i != parseInt(rad.value)) {
      if(element != null) {
        element.style.visibility = "hidden";
      }
    } else {
      if(element != null) {
        element.style.visibility = "visible";
      }
    }
  }
}

Remember to assign numeric values to the radio buttons for this function to work properly.

Answer №2

It appears that you may have made an error:

if (i <= rad.value)

Could you please clarify where the value for rad is coming from?

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The term "Call is not defined" suggests that

I am currently setting up my plugin to accept a callback function as an option argument: (function($) { $.fn.MyjQueryPlugin = function(options) { var defaults = { onEnd: function(e) {} }; var settings = $.extend({ ...

The component is unable to retrieve the state stored within the store

I've encountered an issue with my Array component not being able to access the state "count" stored in a Redux store. I've double-checked that the store is connected and it appears in the React dev tools. The Array component is supposed to take t ...

Using JavaScript to format an array's values within a string in a stylish way

I am facing a challenge in styling words from an array if they are found in a string. My current code is not working as expected. Below is the code I am using, where I am looping through both the array of words and the table containing strings. Only one ...

Analyzing the audio frequency of a song from an mp3 file with the help of HTML5 web audio API

Currently, I am utilizing the capabilities of the HTML5 web audio API to detect when a song's average sound frequency drops below a specific threshold and create corresponding markers. Although I have successfully implemented this using AudioNodes, th ...

Manipulating the contents of a component's state by adding or removing objects

Currently, I am facing an issue with a list of check inputs where when they are selected and deselected, I am attempting to add or remove them from the state. However, I have noticed that when I deselect one item, the previously selected item is the one ...

Restangular failing to apply headers during post requests

I have been encountering an issue while trying to set the header for a single post request using Restangular. Despite following the documentation here and seeking help from a similar question, the request is being sent as plain text instead of JSON. My se ...

Leveraging the power of formula.js within the Vue.js framework

I've encountered an issue while trying to use formulja.js in a Vue project. When I import it and attempt to utilize the PPMT function, an error occurs. Unfortunately, I'm unable to identify the exact nature of the error or how to resolve it. If a ...

Stealthy access using the Facebook API log-in

I'm currently developing an app that features Facebook login functionality. I'm wondering if there's a way to keep a device authorized so that users don't have to go through the process of logging in with Facebook each time they use the ...

Comparing Ember.js yield with Sails.js and Handlebars in a Single Page Application (SPA) approach

I am looking to achieve a specific functionality that is typically done in Ember with Handlebars, but I want to accomplish it using only Sails.js and Handlebars. To set up my Sails project, I used the following command: sails new fooProject --template=han ...

Alter text within a string situated between two distinct characters

I have the following sentence with embedded links that I want to format: text = "Lorem ipsum dolor sit amet, [Link 1|www.example1.com] sadipscing elitr, sed diam nonumy [Link 2|www.example2.com] tempor invidunt ut labore et [Link 3|www.example3.com] m ...

What could be causing my vis.js network's node hover popups to not function properly?

I've encountered an issue where, despite adding the 'title' property to my node objects, the pop up window with the title content doesn't appear when I hover over a node. Here are the options I've chosen and how I've set up m ...

What is the duration of time that broadcast has been in existence?

I have a button on my page. When a user clicks the button, it triggers the following code: as.controller('CustSummary', function($scope, $rootScope, $http, $routeParams, $location) { var loadAbbDetails = function() { ...

Verify the grid of buttons in my code for the background color

My goal is to make sure that when all the buttons are black, the h2 text in the 'lightsoff' div is displayed. If any buttons are not black, the text will remain hidden. I plan to achieve this by creating a new function that checks the background ...

Error: Attempting to access 'props' property of undefined when clicking on a button within a React application leads to a TypeError

I'm currently working on implementing two buttons that will enable navigation to different pages within my React app. However, I encountered an error when attempting to use the button labeled "home." TypeError: Cannot read properties of undefined (rea ...

"AngularJS makes it easy for the logged-in user's information to be accessible and available across

I need to ensure that user information is accessible across all views after logging in. How can I adjust the code to be able to retrieve the pseudonym from another view? Could you provide an example as well? Below is my login controller: app.controller ...

When activating shadow maps in three.js, the alpha value for the clear color will no longer have any effect

After initializing the renderer with alpha: true and setting the clear color using the following code: renderer.setClearColor(0xFFFFFF, 0.6); I noticed that when I enable shadow maps (renderer.shadowMapEnabled = true;), the alpha setting seems to have no ...

Retrieving the data from an HTML button with JavaScript

Currently, I am working on creating a calculator. The HTML and CSS components are complete, but I am facing an issue with getting the button clicks to reflect in the display. As I am still new to JavaScript, I would appreciate any guidance on how to achiev ...

Looking for a pattern that combines Browserify and Angular?

Currently, I am embarking on a project using angular and browserify for the first time. I am seeking advice on how to properly utilize the require function with browserify. There are multiple ways to import files, but so far, I have experimented with the ...

Angular 4 Modals - Enhancing User Experience

Seeking the most effective method to incorporate modals in Angular 4 while adhering to the component architecture. Multiple tutorials and external libraries have been explored, all with limited usage. The common approach involves creating a Modal Service ...

How to extract iframe page title with jQuery in JavaScript

Is there a way to retrieve the title of an iframe's source page and then update the title of the main page? ...