Jump to content

Help with div display changing script


Darkmatter5

Recommended Posts

Here's my HTML:

<table border='0' style='border-collapse: collapse;' width='500'>
  <tr style='border-bottom: 1px solid gray;'>
    <th width='100' align='left'>Armor Maker</th>
    <td align='right'>
      Levels: 
      <a href='javascript:togbld(31);'>1</a> 
      <a href='javascript:togbld(32);'>2</a> 
      <a href='javascript:togbld(33);'>3</a> 
    </td>
  </tr>
  <tr>
    <td colspan='2'>
      <div id='bld31' style='display: block;'>test1 1</div>
      <div id='bld32' style='display: none;'>test2 2</div>
      <div id='bld33' style='display: none;'>test3 3</div>
    </td>
  </tr>
</table>

 

Here's the Javascript:

function togbld(divnum) {
    for (i=1;i<999999;++i) {
        var div=document.getElementById("bld"+i);
        if(div==null) return;
        div.style.display=(i==divnum)?"block":"none";
    }
}

 

This code isn't working, how come??

Link to comment
Share on other sites

Here's my HTML:

<table border='0' style='border-collapse: collapse;' width='500'>
  <tr style='border-bottom: 1px solid gray;'>
    <th width='100' align='left'>Armor Maker</th>
    <td align='right'>
      Levels: 
      <a href='javascript:togbld(31);'>1</a> 
      <a href='javascript:togbld(32);'>2</a> 
      <a href='javascript:togbld(33);'>3</a> 
    </td>
  </tr>
  <tr>
    <td colspan='2'>
      <div id='bld31' style='display: block;'>test1 1</div>
      <div id='bld32' style='display: none;'>test2 2</div>
      <div id='bld33' style='display: none;'>test3 3</div>
    </td>
  </tr>
</table>

 

Here's the Javascript:

function togbld(divnum) {
    for (i=1;i<999999;++i) {
        var div=document.getElementById("bld"+i);
        if(div==null) return;
        div.style.display=(i==divnum)?"block":"none";
    }
}

 

This code isn't working, how come??

 

Dunno, but the code is junk.  You're using the for-loop wrong.  Try this:

<script type="text/javascript">
   window.onload = function()
   {
      var toggles = new Array();
      var panes = new Array();
      var links = document.getElementsByTagName('a');
      var divs = document.getElementsByTagName('div');

      for(var i = 0; i < links.length; i++)
      {
         if(links.getElementById('toggle' + i))
         {
            toggles[i] = links.getElementById('toggle' + i);
         }
      }

      for(var j = 0; j < divs.length; j++)
      {
         if(divs.getElementById('bld' + j))
         {
            panes[j] = divs.getElementById('bld' + j);
         }
      }

      for(var k = 0; k < toggles.length; k++)
      {
         toggles[k].onclick = function()
         {
            panes[k].style.display = (panes[k].style.display == 'none') ? 'block' : 'none';
         }
      }
   }
</script>

<!-- ... -->

<table border='0' style='border-collapse: collapse;' width='500'>
  <tr style='border-bottom: 1px solid gray;'>
    <th width='100' align='left'>Armor Maker</th>
    <td align='right'>
      Levels: 
      <a id='toggle31' href='#'>1</a> 
      <a id='toggle32' href='#'>2</a> 
      <a id='toggle33' href='#'>3</a> 
    </td>
  </tr>
  <tr>
    <td colspan='2'>
      <div id='bld31' style='display: block;'>test1 1</div>
      <div id='bld32' style='display: none;'>test2 2</div>
      <div id='bld33' style='display: none;'>test3 3</div>
    </td>
  </tr>
</table>

 

Something like this should work, assuming you have the same number as links as you do divs, and it wouldn't hurt to keep their id's lined up (i.e., toggle31 is used for bld31, etc).

 

Keep in mind that I haven't tested this, so don't expect it to work properly just by cutting and pasting.  You may need to play with it.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.