• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1078
  • Last Modified:

Toggle multiple DIV items from one link - but not ALL divs

Fabulously simple solution - but being a complete novice at scripting, I need to know how to alter the code in the solution so that only divs of a specific CSS class are hidden. For example I'd like to make use of this function in two different places on a single page without one affecting the other.

I'm assuming the fix is simple, but don't know the syntax.

thanks


I need help with the following
Sequence:
1) Detect all DIV items within a page
2) Create an array of the names (id's) of all DIV items collected
3) Test each name against the name passed into the function (div id argument)
4) If the name matches, show DIV, toggle all the rest

... oh, and I need to be able to do this with one click of a link :)
Here is my code. I am ready to strangle it. Please help:

<script language="javascript">

function divshowhide(divid){
   var alldivsonpage = document.getElementsByTagName('div');
   var divArray = alldivsonpage;
   var divnameArray = new Array();
   var dts = document.getElementById('divid');


      for(i=0;i<divArray.length;i++) {
             //next, for each div counted, get name of div
            //here is where i get into trouble
            divnameArray = "The name of each div element in divArray(i)";//how to access it?
      }

            
      for(x=0;i<divnameArray.length;x++) {
      
            if ( divnameArray(x) == dts )
                  dts.style.display = 'block';
                              
            } else {
                  document.getElementById(divnameArray(x)).style.display='none';
            
            }
            
      }
}


</script>

Links and divs:
<a href="javascript:;"onmousedown="divshowhide(idd);">layer one</a><br>
<div id="idd" style="display:none">
This is the first layer
</div>

Thanks in advance
0
dangerjames
Asked:
dangerjames
  • 3
  • 2
1 Solution
 
hieloCommented:
try this:
<script language="javascript"> 
function divshowhide(divid){
	var divArray = document.getElementsByTagName('div');
	for( var i=0; i < divArray.length; ++i)
	{
		divArray[i].style.display = (divArray[i].id==divid ? "" : "none"); 
	}
} 

</script> 
Links and divs:
<a href="javascript:;" onclick="divshowhide('idd');">layer one</a><br>
<div id="idd" style="display:none">
This is the first layer
</div>

Open in new window

0
 
dangerjamesAuthor Commented:
Sorry - I don't think I was really clear.

the code in the example above wasn't mine - it was what was attached to the solution that seemed to put me on the right path, which is very similar to what you posted. Here's the original solution:

<script language="javascript">
function divshowhide(divid)
{
   var divArray = document.getElementsByTagName('div');
      for(i=0;i<divArray.length;i++)
      {
            if(divArray[i].id == divid)
      divArray[i].style.display = 'block';
      else
      divArray[i].style.display = 'none';
      }
}
</script>


But my problem differs in the following way - I have several small divs each containing text under a blank area on my page. Each bit of text is a link. When a link is clicked I'd like the corresponding div to appear in that blank area. So when a second link is clicked, the original one should disappear and the appropriate new one should appear. I have two such areas on the page - the divs that are to appear in each of the areas are assigned separate class names (ie  .pressdiv and .statementdiv)

All of the divs are nested in a simple 3 column table -so what's happening with the code shown above is that when any link using the showhide function is clicked everything disappears except the background wallpaper. Whoosh.

I assume that this means that my Array isn't defined specifically enough. But then that's where I need the help.

Thanks - I appreciate the quick response!


0
 
Matthew KellyCommented:
It would help if you could post the HTML with the div's
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
dangerjamesAuthor Commented:
Here is the code from the body of the page (sorry I couldn't include it before - working on different machines without access between them most of the weekend).



</td>
<td class="tdmid" align="center" width="680" height="600">
		<div style="position:relative;width:680px;height:700px;margin:auto;-adbe-g:m;">
			border="0" /></div>		//followed by several divs with small images, etc.
 
							//the following 3 divs are containers for individual text based links
		
		<div class="swapLinkdiv" style="position:absolute;top:571px;left:363px;width:120px;height:16px;-adbe-c:c">
			<a href="javascript:;" onclick="divshowhide('commissions');">Recent Commissions</a></div>
		<div class="swapLinkdiv" style="position:absolute;top:571px;left:517px;width:100px;height:16px;-adbe-c:c">
			<a href="javascript:;" onclick="divshowhide('grants');">Grants &amp; Awards</a></div>
		<div class="swapLinkdiv" style="position:absolute;top:592px;left:391px;width:212px;height:16px;-adbe-c:c">
			<a href="javascript:;" onclick="divshowhide('quotes');">just a few cool quotes from the press</a></div>
		
 
		<div id="quotes" class="pressdiv" style="position:absolute;top:125px;left:360px;width:306px;height:429px;-adbe-c:c">
			<h5>Quotes</h5>
			<br /> //  blah blah blah  all the text that should show when the quotes divshowhide link is clicked
 
		</div>
 
		<div id="grants" class="pressdiv" style="position:absolute;top:125px;left:360px;width:306px;height:437px;-adbe-c:c">
			<h5>Grants and Awards</h5>
			<br />//  blah blah blah  all the text that should show when the grants divshowhide link is clicked
			
		</div>
								
<div id="commissions" class="pressdiv" style="position:absolute;top:125px;left:360px;width:306px;height:437px; -adbe-c:c">
			<h5>Commissions</h5>
			<br />/>//  blah blah blah  all the text that should show when the commissions divshowhide link is clicked
			
		</div>
</td>

Open in new window

0
 
Matthew KellyCommented:
Try this code
<html>
<head>
<script language="javascript">
 
function divshowhide(divid)
{
	var alldivsonpage = document.getElementsByTagName('div');	
            
	for(x=0;x<alldivsonpage.length;x++) 
	{
		if ( alldivsonpage[x].className == "pressdiv" )
		{      
			if ( alldivsonpage[x].id == divid ) 
			{
				document.getElementById(alldivsonpage[x].id).style.display = 'block';
			} 
			else 
			{
				document.getElementById(alldivsonpage[x].id).style.display='none';
			}
		}
            
	}
}
</script>
</head>
<body onload="divshowhide('commissions')">
<div style="position:relative;width:680px;height:700px;margin:auto;"></div>
                
                <div class="swapLinkdiv" onclick="divshowhide('commissions');" style="font-style:underline;color:blue;cursor:hand;cursor:pointer;position:absolute;top:571px;left:363px;width:120px;height:16px;">
                        Recent Commissions</div>
                <div class="swapLinkdiv" onclick="divshowhide('grants');" style="font-style:underline;color:blue;cursor:hand;cursor:pointer;position:absolute;top:571px;left:517px;width:100px;height:16px;">
                        Grants &amp; Awards</div>
                <div class="swapLinkdiv" onclick="divshowhide('quotes');" style="font-style:underline;color:blue;cursor:hand;cursor:pointer;position:absolute;top:592px;left:691px;width:212px;height:16px;">
                        just a few cool quotes from the press</div>
                
 
                <div id="quotes" class="pressdiv" style="position:absolute;top:125px;left:360px;width:306px;height:429px;">
                        <h5>Quotes</h5>
                        <br /> blah blah blah  all the text that should show when the quotes divshowhide link is clicked
 
                </div>
 
                <div id="grants" class="pressdiv" style="position:absolute;top:125px;left:360px;width:306px;height:437px;">
                        <h5>Grants and Awards</h5>
                        <br />blah blah blah  all the text that should show when the grants divshowhide link is clicked
                        
                </div>
                                                                
		<div id="commissions" class="pressdiv" style="position:absolute;top:125px;left:360px;width:306px;height:437px;">
                        <h5>Commissions</h5>
                        <br />blah blah blah  all the text that should show when the commissions divshowhide link is clicked
                        
                </div>
</body>
</html>

Open in new window

0
 
dangerjamesAuthor Commented:
absolutely perfect (and I was very close to the solution on my own, but would not have gotten the syntax right).

thanks!
0

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now