/    Sign up×
Community /Pin to ProfileBookmark

hide div when radio button changes

I am trying to hide some content when a radio button is changed. I have been searching the internet for this, but the only thing I found that is close to what I wanted was done in .asp. I tried to convert it, but seeing I do not know Javascript very well, I was unsuccessful. I’m hoping someone here can help me set it right.

[CODE]
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>

<head>
<meta content=”text/html; charset=utf-8″ http-equiv=”Content-Type” />
<title>Untitled 1</title>

<script language=”javascript”>
window.onload=function(){
var rb = document.getElementsById(‘opt0’);
var d = document.getElementById(‘dvArea’);
if (rb.checked == true) {

d.style.visibility = “visible”;
}
else {
d.style.visibility = “hidden”;
}
};
</script>
</head>

<body>
<form id=”frmOptions”>
<input type=”radio” name=”options” id=”opt0″ checked=”checked” />
<label for=”opt0″>Option 1</label>
<input type=”radio” name=”options” id=”opt1″ />
<label for=”opt1″>Option 2</label>
</form>
<div id=”dvArea”>
Some contents
</div>
</body>

</html
[/CODE]

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@vwphillipsJul 26.2013 — [CODE] <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>

</head>

<body>
<form id="frmOptions">
<input type="radio" name="options" id="opt0" checked="checked" onclick="check()"/>
<label for="opt0">Option 1</label>
<input type="radio" name="options" id="opt1" onclick="check()" />
<label for="opt1">Option 2</label>
</form>
<div id="dvArea">
Some contents
</div>
<script language="javascript">

function check(){
document.getElementById('dvArea').style.visibility =document.getElementById('opt0').checked?"visible":"hidden";
}

check();
</script>
</body>

</html>


[/CODE]
Copy linkTweet thisAlerts:
@cootheadJul 26.2013 — Hi there cinci-hal,

Our friend [b]Vic[/b] supplied a solution whilst I was preparing mine. ?

One minor difference in mine is that, I hide the form from those with javascript disabled.

I also remove event handlers from the HTML. ?

[color=navy]
&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;

&lt;meta charset="utf-8"&gt;

&lt;title&gt;untitled document&lt;/title&gt;

&lt;style&gt;
#container {
width:960px;
margin:auto;
}
.hide{
display:none;
}
#frmOptions label {
cursor:pointer;
}
&lt;/style&gt;

&lt;script&gt;
(function() {
'use strict';

function init(){

document.getElementById('frmOptions').className='';
var obj=document.getElementById('dvArea');

var inp=document.getElementById('frmOptions').getElementsByTagName('input');
var c=inp.length;

while(--c&gt;-1) {
inp[c].number=c;
inp[c].onclick=function(){

this.number==0?obj.className='':obj.className='hide';
}
}
}
window.addEventListener?
window.addEventListener('load',init,false):
window.attachEvent('onload',init);
})();
&lt;/script&gt;

&lt;/head&gt;
&lt;body&gt;

&lt;div id="container"&gt;

&lt;form id="frmOptions" class="hide"&gt;
&lt;input type="radio" name="options" id="opt0" checked="checked"&gt;
&lt;label for="opt0"&gt;Option 1&lt;/label&gt;
&lt;input type="radio" name="options" id="opt1" &gt;
&lt;label for="opt1"&gt;Option 2&lt;/label&gt;
&lt;/form&gt;

&lt;div id="dvArea"&gt;
&lt;p&gt;
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec
ultricies sollicitudin nisi, ut molestie felis adipiscing sit
amet. Sed auctor vehicula commodo. Nam a nibh neque, vitae
faucibus felis. Vivamus at metus eget eros consequat fringilla.
Quisque magna magna, laoreet eu tempus sit amet, fringilla at
tellus. Praesent felis tortor, scelerisque vitae fringilla
non, porttitor et lacus. Ut ut sapien nec quam tincidunt
consectetur in nec lacus.
&lt;/p&gt;
&lt;/div&gt;

&lt;/div&gt;

&lt;/body&gt;
&lt;/html&gt;[/color]


[i]coothead[/i]
Copy linkTweet thisAlerts:
@cinci-halauthorJul 26.2013 — That is perfect! Thank you so much!
Copy linkTweet thisAlerts:
@cootheadJul 26.2013 — Hi there cinci-hal,

[indent]

I don't know which code took your fancy, but I will say...

[indent]

no problem, you're very welcome. ?

[/indent]


...for both of us. ?

[/indent]


[i]coothead[/i]
×

Success!

Help @cinci-hal 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.17,
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,
)...