Jump to content

Javascript div hiding toggle not "toggling"


DaveLinger

Recommended Posts

Here's the code "in action":

http://www.dragonforceusa.com

just click "There are x comments for this post", and it will toggle the style of "commentsx" from "show:none" (in-line) to "show:block". Problem is, it doesn't toggle back when I click the link again, and it should. Here is the javascript:

[code]function toggleLayer(whichLayer)
{
if (document.getElementById)
{
// this is the way the standards work
var style2 = document.getElementById(whichLayer).style;
style2.display = style2.display? "":"block";
}
else if (document.all)
{
// this is the way old msie versions work
var style2 = document.all[whichLayer].style;
style2.display = style2.display? "":"block";
}
else if (document.layers)
{
// this is the way nn4 works
var style2 = document.layers[whichLayer].style;
style2.display = style2.display? "":"block";
}
}[/code]
Update: just found out that it is the javascript file that is causing this to crash, here's the code:

[code]function toggleLayer(whichLayer)
{
if (document.getElementById)
{
// this is the way the standards work
var style2 = document.getElementById(whichLayer).style;
style2.display = style2.display? "":"block";
}
else if (document.all)
{
// this is the way old msie versions work
var style2 = document.all[whichLayer].style;
style2.display = style2.display? "":"block";
}
else if (document.layers)
{
// this is the way nn4 works
var style2 = document.layers[whichLayer].style;
style2.display = style2.display? "":"block";
}
}[/code]
use with your 'whichLayer' (I assume you are passing the item id in 'whichlayer')
[code]// makes element disappear
document.getElementById('whichLayer').style.display = 'none';
// makes element display in default format for element
document.getElementById('whichLayer').style.display = '';  [/code]
gah, I'm sorry but I can't get it to work... here's the code in my .js file:

[code]
function toggleLayer(whichLayer)
{
// makes element display in default format for element
document.getElementById('whichLayer').style.display = 'block';
// makes element disappear
document.getElementById('whichLayer').style.display = 'none';
}
[/code]

I switched the order and changed the "appear" code to "block" instead of default, because default is "none". It starts hidden. An example link is "javascript:toggleLayer('comments6');", with comments6 being a div with style display:none
[code]function toggleLayer(whichLayer)
{
// makes element display in default format for element
if (document.getElementById('whichLayer').style.display == 'none')
    document.getElementById('whichLayer').style.display = 'block';
// makes element disappear
if (document.getElementById('whichLayer').style.display == 'block')
    document.getElementById('whichLayer').style.display = 'none';
}[/code]
[quote author=mainewoods link=topic=124258.msg515669#msg515669 date=1170018103]
[code]function toggleLayer(whichLayer)
{
// makes element display in default format for element
if (document.getElementById('whichLayer').style.display == 'none')
    document.getElementById('whichLayer').style.display = 'block';
// makes element disappear
if (document.getElementById('whichLayer').style.display == 'block')
    document.getElementById('whichLayer').style.display = 'none';
}[/code]
[/quote]

still not working, here's the url of the code in action:

http://www.dragonforceusa.com/

Thanks for your help so far.
Try this

[code]
function toggle(id)
{
var layer = document.getElementById(id);
if(layer.style.display == 'none')
layer.style.display = 'block';
else
layer.style.display = 'none';
}
<a href="javascript: toggle('throughfireandflames')">Test</a>
<div id="throughfireandflames' name='throughfireandflames' style="display:none;">
Test
</div>
[/code]

PS> Dragonforce is a awesome band =).

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.