Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiple divs?

Status
Not open for further replies.

youradds

Programmer
Joined
Jun 27, 2001
Messages
817
Location
GB
Hi,

I have the following bit of code;

Code:
              <script language="javascript"> 
              <!-- 
              var state = 'none'; 

              function showhide(layer_ref) { 

              if (state == 'block') { 
              state = 'none'; 
              } 
              else { 
              state = 'block'; 
              } 
              if (document.all) { //IS IE 4 or 5 (or 6 beta) 
              eval( "document.all." + layer_ref + ".style.display = state"); 
              } 
              if (document.layers) { //IS NETSCAPE 4 or below 
              document.layers[layer_ref].display = state; 
              } 
              if (document.getElementById &&!document.all) { 
              hza = document.getElementById(layer_ref); 
              hza.style.display = state; 
              } 
              } 
              //--> 
              </script>

...which works greate with hiding/showing a div layers. i.e;

<a onclick="showhide('div_233')">

This works fine ... if using;

<div id="div_233">

... but how could I change it to work with;

Code:
<div id="div_233.1">..</div>
<div id="div_233.2">..</div>
<div id="div_233.3">..</div>
<div id="div_233.4">..</div>
<div id="div_233.5">..</div>
..etc

So in short, I need to make all of these "divs" visable when using the showhide() routine.

Any suggestions? :)

TIA
 
Code:
<script language="javascript"> 
<!-- 
var state = 'none'; 
var numDivs = 9; // change this to the number of divs

function showhide(layer_ref) { 

  for ( var x = 1; x <= numDivs; x++ ) {
    if (state == 'block') {
      state = 'none'; 
    } else { 
      state = 'block'; 
    } 
    if (document.all) { //IS IE 4 or 5 (or 6 beta) 
      document.all[layer_ref+x]style.display = state; 
    } 
    if (document.layers) { //IS NETSCAPE 4 or below 
      document.layers[layer_ref+x].display = state; 
    } 
    if (document.getElementById &&!document.all) { 
      hza = document.getElementById(layer_ref+x); 
      hza.style.display = state; 
    }
  }
} 
//--> 
</script>
Notice a couple of things:

I took out the use of eval in that one statement. I don't think you need it.

I did NOT put a dot (period) in the name of the divs. I don't think it's allowed (I never do it, so I'm not certain).


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
try something like:

Code:
// get all divs
var allDivs = document.getElementsByTagName('div');

// loop through all divs
for (var i = 0; i < allDivs.length; i++) {
    
    // if the div id starts with div_233
    if (allDivs[i].id.indexOf('div_233') > -1) {

        // hide or show div - put your code here

    }
}

*cLFlaVA
----------------------------
[tt]a frickin' twelve-gauge, what do you think?[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
[banghead]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top