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]
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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
Link to comment
Share on other sites

[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]
Link to comment
Share on other sites

[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.
Link to comment
Share on other sites

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 =).
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.