...

View Full Version : Show Hide Divide onchange of input



Chrys
03-12-2012, 03:19 AM
This code works to show divide with id "it" when select option "it" is chosen and it shows divide "de" when option "de" is chosen.

Problem: it will not show the nested divides inside the "it" and "de" divides. It only shows the <p></p> tags. Does someone know how I can get it to show the divides inside the main divide as well?

Thanks in advance for your help.


Head


function showstuff(divID)
{
els = document.getElementById( 'mydiv' ).getElementsByTagName( 'div' );

for (var i = 0; i < els.length; i++)
{
els[i].style.visibility="hidden";
els[i].style.display="none";
}

if( divID )
{
document.getElementById(divID).style.visibility="visible";
document.getElementById(divID).style.display="block";
}

if( window.event )
window.event.returnValue = false;

return false;
}
window.onload=function()
{
showstuff('');
}

Body


<select size="1" hide_label="0" id="experience" class=" validate['required']" title="" type="select" name="experience" onchange="showstuff(this.value) ;">
<option value="">(Please select)</option>
<option value="it">IT</option>
<option value="de">Design Engineer</option>
<option value="both">BOTH</option>
</select>

<div id="mydiv">

<div id="it">
<p>why dont you show anything else!</p>

<div class="ccms_form_element cfdiv_header" id="_h3_it_disciplines_check_all_that_apply_h3___container_div"><h3>IT Disciplines (Check all that apply)</h3><div class="clear"></div></div>
</div>
<div id="de">

<p>why dont you show anything else! UGH!!</p>
<div class="ccms_form_element cfdiv_header" id="_h3_design_engineer_disciplines_check_all_that_apply_h3___container_div"><h3>Design Engineer Disciplines (Check all that apply)</h3><div class="clear"></div></div>
<p>why dont you show anything else!</p>
</div>
</div>

devnull69
03-12-2012, 08:05 AM
It's all in your code. If you examine it step by step, you'll notice that

first you hide every single DIV (including the nested inner DIVs). Then you only show one specific DIV based on its id. But the nested inner DIVs will still be hidden.

Solution: Only hide the outer DIVs in the first place. The nested inner DIVs will automatically be hidden together with the outer DIVs. Then if you show one of the outer DIVs, its inner DIVs will automatically show, too.



var outerDIVs = ["it", "de"]; // add more id's for outer DIVs here
function showstuff(divID)
{
for (var i = 0; i < outerDIVs.length; i++)
{
document.getElementById(outerDIVs[i]).style.display="none";
}

if( divID )
{
document.getElementById(divID).style.display=""; // this will set display to default
}

if( window.event )
window.event.returnValue = false;

return false;
}

Chrys
03-12-2012, 03:44 PM
Thank you so much for your reply. That makes a lot of sense.

However, I tried implementing those changes and now I am getting an error:
Uncaught ReferenceError: showstuff is not defined

Chrys
03-14-2012, 04:58 PM
Got it now thank you! :)

Is there any way that I can have it show all divides if a certain option is chosen?

devnull69
03-14-2012, 09:18 PM
Just put the following for loop into a function and call this function from the desired action (like onclick of a button etc.)


for (var i = 0; i < outerDIVs.length; i++)
{
document.getElementById(outerDIVs[i]).style.display="";
}

Chrys
03-15-2012, 11:59 PM
Just put the following for loop into a function and call this function from the desired action (like onclick of a button etc.)


for (var i = 0; i < outerDIVs.length; i++)
{
document.getElementById(outerDIVs[i]).style.display="";
}


I am confused by this... please forgive me if I am being silly.

But I am calling the showstuff function onchange for the select. The function above would have to be called using a separate button not an option within the select I am already using. Right?

I think I just need to add another line saying something like this:

If divID = "both" show "de" and "it"

I am just having trouble with the syntax of this.

webdev1958
03-16-2012, 12:24 AM
I think I just need to add another line saying something like this:

If divID = "both" show "de" and "it"

I am just having trouble with the syntax of this.

I would do something like this:



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title></title>
<style type="text/css">
#it,#de{
display: none;
}
</style>
</head>
<body>
<select size="1" id="experience" name="experience" onchange="showstuff(this) ;">
<option value="">(Please select)</option>
<option value="it">IT</option>
<option value="de">Design Engineer</option>
<option value="both">BOTH</option>
</select>
<div id="mydiv">
<div id="it" class="showHideDivs">
<p>IT content</p>
</div>
<div id="de" class="showHideDivs">
<p>DE content</p>
</div>
</div>
<script type="text/javascript">
var showHideDivsO = document.getElementsByClassName('showHideDivs');
function showstuff(selO){
showHideAll('none'); //first hide all the divs
if(selO.selectedIndex == 0){return;}
if(selO.value == 'both'){ //show all the divs
showHideAll('block');
} else { //show just the selected div
document.getElementById(selO.value).style.display='block';
}
}
function showHideAll(status){
if(status != 'none' && status != 'block'){status == 'block';}
for(i=0; i<showHideDivsO.length; i++){
showHideDivsO[i].style.display=status;
}
}
</script>
</body>
</html>

Chrys
03-16-2012, 01:53 AM
That is a lot simpler than I was thinking. That makes a lot of sense. Thank you so much for your help with this! :)

webdev1958
03-16-2012, 02:30 AM
glad it's sorted :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum