/    Sign up×
Community /Pin to ProfileBookmark

show and hide radio button

I have to 2 radio buttons and two hidden divs. How would I be able to show and hide them depending on the selection I’ve chosen?

Can anyone point me to the right direction??

Thanks

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@edatzSep 17.2010 — Here's something I use now and then. I didn't write it but you could use it as a base to work from.
[CODE]<html>
<head>
<title>Click and show</title>
<style type="text/css">body {font-family: Verdana;}</style>
<script language="Javascript">
<!-- Begin
// -- Contact verify
function button_show ( )
{ valid = true;
if ( document.tlform.Name.value == "" )
{alert ( "Please click a radio button for the nature of your request." ); valid = false;}
return valid;}
var message="";
function clickIE() {if (document.all) {(message);return false;}}
function clickNS(e) {if (document.layers||(document.getElementById&&!document.all)) {
if (e.which==2||e.which==3) {(message);return false;}}}
if (document.layers) {document.captureEvents(Event.MOUSEDOWN);document.onmousedown=clickNS;}
else{document.onmouseup=clickNS;document.oncontextmenu=clickIE;}
document.oncontextmenu=new Function("return false")
function showdetails(el) {
if (el.value == "Edit") {document.getElementById('text').style.display = "block";}
else {document.getElementById('text').style.display = "none";
document.getElementById('eded').value = ''; } }
// End -->
</script>
</head>
<body>
<div align="center">
<p>Radio Button Option Drop Down</p>
<form name="tlform" action="#" method="post" onSubmit="return button_show ( );">
<small><b>Pick an option: </b> <br>
Yes <input type="radio" name="request" value="Edit" onclick="showdetails(this);"> -
No <input type="radio" name="request" value="Delete" onclick="showdetails(this);" checked></small><br>

<div id="text" style="display:none;" align=center><small>Some kind of text.</small><br>
<input type="text" name="editdetails" class="iparea" id="eded" value="">
<br>
The form element could be anything.<br>
Just insert the code into a form.
</div>
</form>
</div>
</body></html>[/CODE]


Hope it helps
Copy linkTweet thisAlerts:
@oasmatauthorSep 17.2010 — It works good but I also need the Second radio button to hide one DIV and Show another.
Copy linkTweet thisAlerts:
@JMRKERSep 17.2010 — It works good but I also need the Second radio button to hide one DIV and Show another.[/QUOTE]

Is this what you are after? ...
<i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;title&gt;RBtn - Div Show/Hide&lt;/title&gt;
&lt;style type="text/css"&gt;
#DIVsect0 { display:none; background-color:yellow; border:1px solid cyan; }
#DIVsect1 { display:none; background-color:orange; border:3px dotted blue; }
&lt;/style&gt;

&lt;script type="text/javascript"&gt;
// From: http://www.webdeveloper.com/forum/showthread.php?t=235831

function RBtnSet(Info,flag) {
var CState = document.getElementById(Info);
if (flag) { CState.style.display = "block"; }
else { CState.style.display = "none"; }
}

&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;label&gt;&lt;input type="radio" name="RBtnControl"
onclick="RBtnSet('divSect0',true);RBtnSet('divSect1',false)"&gt;DIV section 1&lt;/label&gt;
&lt;label&gt;&lt;input type="radio" name="RBtnControl"
onclick="RBtnSet('divSect0',false);RBtnSet('divSect1',true)"&gt;DIV section 2&lt;/label&gt;
&lt;p&gt;
&lt;div id="divSect0"&gt; DIV section #1 &lt;/div&gt;
&lt;div id="divSect1"&gt; DIV section #2 &lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@edatzSep 17.2010 — Or, slightly different than JMRKER's in that you have one checked for a no show and you have to define the number of "shows" (divs). This one I used for something different [I](a set of instructions with about 12 divs and links instead of buttons.)[/I] You could have as many buttons as you want - even inside of a show div).
[CODE]<html>
<head>
<title>RoundTable Manual</title>
<style type="text/css">
body {font-family: Verdana;}
.rslt {display:none; border:1px solid; width:200px;}
</style>
<script type="text/javascript">
function rd(id) {
/// -- <=4 -- number match number of div results
for (i=1;i<=4;i++) {

var divname = 'div'+i;
var divStyle = document.getElementById(divname).style;
divStyle.display=(id==divname)?'block':'none';
}
}
</script>
</head>
<body><div align="center">
<form>
<b>Radio Show</b><br>
No <input type="radio" name="show" onClick="rd('div1');" checked>
One <input type="radio" name="show" onClick="rd('div2');">
Two <input type="radio" name="show" onClick="rd('div3');">

<div id="div1">&nbsp;</div>
<div id="div2" class="rslt"><b>One</b></div>
<div id="div3" class="rslt"><b>Two</b><br>
Three <input type="radio" name="show" onClick="rd('div4');"></div>

<div id="div4" class="rslt"><b>Three</b></div>
</form>
</body>
</html>[/CODE]

Note the "number match number of div results" and line under it. The number 4 would be changed ot as many divs as you are going to use.

Well, that oughta keep you occupied for a while ?
×

Success!

Help @oasmat spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...