uncheck radio button, close div...

Hi everyone...

I'm currently using this script to display some divs:

var dArray=new Array("div1","div2","div3");
function showThis(nme) {
   for (var i=0;i<dArray.length;i++) {
      document.getElementById(dArray[i]).style.display='none';
   }
   document.getElementById(nme).style.display='';
}

<input type="radio" value="div1" onclick="showThis(this.value);" onmouseover="this.style.cursor='hand'"  name="forms">
<DIV ID="div1" STYLE="display:none">
blah
</DIV>
<input type="radio" value="div2" onclick="showThis(this.value);" onmouseover="this.style.cursor='hand'"  name="forms">
<DIV ID="div2" STYLE="display:none">
blah
</DIV>
<input type="radio" value="div3" onclick="showThis(this.value);" onmouseover="this.style.cursor='hand'"  name="forms">
<DIV ID="div3" STYLE="display:none">
blah
</DIV>

Can it be modified so that user can "turn off" the radio button, therefore hiding the div?

Thanks as always  :-)
whaleykAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mosphatCommented:
How about this?

<script>
var dArray=new Array("div1","div2","div3");
var activeElement = null;
function showThis(nme) {
   for (var i=0;i<dArray.length;i++) {
      document.getElementById(dArray[i]).style.display='none';
   }
     if (activeElement == nme) {
        activeElement = null;
        nme.checked = false;
    } else {
        activeElement = nme;
        document.getElementById(nme.value).style.display='';
    }
}
</script>
<input type="radio" value="div1" onclick="showThis(this);" onmouseover="this.style.cursor='hand'"  name="forms">
<DIV ID="div1" STYLE="display:none">
blah
</DIV>
<input type="radio" value="div2" onclick="showThis(this);" onmouseover="this.style.cursor='hand'"  name="forms">
<DIV ID="div2" STYLE="display:none">
blah
</DIV>
<input type="radio" value="div3" onclick="showThis(this);" onmouseover="this.style.cursor='hand'"  name="forms">
<DIV ID="div3" STYLE="display:none">
blah
</DIV>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
devicCommented:
hi whaleyk, check this:
==================
<script>

var dArray=new Array("div1","div2","div3","off");
function showThis(obj)
{
      for(var i=0;i<dArray.length;i++)
      document.getElementById(dArray[i]).style.display='none';
      if(obj.value=="none")return;
      document.getElementById(obj.value).style.display='';
      document.getElementById("off").style.display='';
}
</script>

<input type="radio" value="div1" onclick="showThis(this);" onmouseover="this.style.cursor='hand'"  name="forms">
<DIV ID="div1" STYLE="display:none">
blah1
</DIV><br>
<input type="radio" value="div2" onclick="showThis(this);" onmouseover="this.style.cursor='hand'"  name="forms">
<DIV ID="div2" STYLE="display:none">
blah2
</DIV><br>
<input type="radio" value="div3" onclick="showThis(this);" onmouseover="this.style.cursor='hand'"  name="forms">
<DIV ID="div3" STYLE="display:none">
blah3
</DIV><br>
<DIV ID="off" STYLE="display:none">
"turn off"
<input type="radio" value="none" onclick="showThis(this);" onmouseover="this.style.cursor='hand'"  name="forms">
</DIV>
0
ZvonkoSystems architectCommented:
Like this:

<script>

var dArray=new Array("div1","div2","div3");
function showThis(theRadio) {
   hideAll();
   document.getElementById(theRadio.value).style.display='';
}

function hideAll(){
   for (var i=0;i<dArray.length;i++) {
      document.getElementById(dArray[i]).style.display='none';
   }
}
</script>
<form>
<input type="radio" value="div1" onclick="showThis(this);" onDblClick="this.checked=false;hideAll()" style="cursor:hand;"  

name="forms">
<DIV ID="div1" STYLE="display:none">
blah
</DIV>
<input type="radio" value="div2" onclick="showThis(this);" onDblClick="this.checked=false;hideAll()" style="cursor:hand;"  

name="forms">
<DIV ID="div2" STYLE="display:none">
blah
</DIV>
<input type="radio" value="div3" onclick="showThis(this);" onDblClick="this.checked=false;hideAll()" style="cursor:hand;"  

name="forms">
<DIV ID="div3" STYLE="display:none">
blah
</DIV>
</form>

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

ZvonkoSystems architectCommented:
Here a simplified version:

<script>
var lastTurn;
var dArray=new Array("div1","div2","div3");
function showThis(theRadio) {
   for (var i=0;i<dArray.length;i++) {
      document.getElementById(dArray[i]).style.display='none';
   }
   if(lastTurn!=theRadio){
     document.getElementById(theRadio.value).style.display='';
   } else {
     theRadio.checked = false;
   }
   lastTurn = theRadio;
}

</script>
<form>
<input type="radio" value="div1" onclick="showThis(this);" style="cursor:hand;" name="forms">
<DIV ID="div1" STYLE="display:none">
blah
</DIV>
<input type="radio" value="div2" onclick="showThis(this);" style="cursor:hand;" name="forms">
<DIV ID="div2" STYLE="display:none">
blah
</DIV>
<input type="radio" value="div3" onclick="showThis(this);" style="cursor:hand;" name="forms">
<DIV ID="div3" STYLE="display:none">
blah
</DIV>
</form>

0
ZvonkoSystems architectCommented:
Sorry, now I see my last proposal is simmelar to mosphat first proposal.
0
mosphatCommented:
With the difference that once you turn off a selection, your code doesn't allow a re-selection. Mine does ;)
0
ZvonkoSystems architectCommented:
Uhps, that too :(
0
whaleykAuthor Commented:
Thanks all so much (mosphat  you were first and your code does exactly what I need) :-)

THANK YOU THANK YOU THANK YOU
0
ZvonkoSystems architectCommented:
Only for philosofical discussions: document.activeElement is a default attribute.
So perhaps changing that var name would avoid confusion.

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.