DaveLinger Posted January 27, 2007 Share Posted January 27, 2007 Here's the code "in action":http://www.dragonforceusa.comjust 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 workvar style2 = document.getElementById(whichLayer).style;style2.display = style2.display? "":"block";}else if (document.all){// this is the way old msie versions workvar style2 = document.all[whichLayer].style;style2.display = style2.display? "":"block";}else if (document.layers){// this is the way nn4 worksvar style2 = document.layers[whichLayer].style;style2.display = style2.display? "":"block";}}[/code] Link to comment https://forums.phpfreaks.com/topic/35921-javascript-div-hiding-toggle-not-toggling/ Share on other sites More sharing options...
DaveLinger Posted January 27, 2007 Author Share Posted January 27, 2007 also here's where i found the code (with the example that toggles):http://www.netlobo.com/div_hiding.htmlOH CRAP, I just noticed that the whole page is blank in IE! Please help!http://www.dragonforceusa.com/ Link to comment https://forums.phpfreaks.com/topic/35921-javascript-div-hiding-toggle-not-toggling/#findComment-170357 Share on other sites More sharing options...
DaveLinger Posted January 28, 2007 Author Share Posted January 28, 2007 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 workvar style2 = document.getElementById(whichLayer).style;style2.display = style2.display? "":"block";}else if (document.all){// this is the way old msie versions workvar style2 = document.all[whichLayer].style;style2.display = style2.display? "":"block";}else if (document.layers){// this is the way nn4 worksvar style2 = document.layers[whichLayer].style;style2.display = style2.display? "":"block";}}[/code] Link to comment https://forums.phpfreaks.com/topic/35921-javascript-div-hiding-toggle-not-toggling/#findComment-170870 Share on other sites More sharing options...
mainewoods Posted January 28, 2007 Share Posted January 28, 2007 This way works in both ff and ie:[code]object.style.display = 'none'; // disappearsobject.style.display = ''; // displays in default format for item[/code] Link to comment https://forums.phpfreaks.com/topic/35921-javascript-div-hiding-toggle-not-toggling/#findComment-170883 Share on other sites More sharing options...
DaveLinger Posted January 28, 2007 Author Share Posted January 28, 2007 Hm...Sorry for being a total Javascript noob, but how would I go about implementing that code? It doesn't specify which object it's hiding. Link to comment https://forums.phpfreaks.com/topic/35921-javascript-div-hiding-toggle-not-toggling/#findComment-171047 Share on other sites More sharing options...
mainewoods Posted January 28, 2007 Share Posted January 28, 2007 use with your 'whichLayer' (I assume you are passing the item id in 'whichlayer')[code]// makes element disappeardocument.getElementById('whichLayer').style.display = 'none';// makes element display in default format for element document.getElementById('whichLayer').style.display = ''; [/code] Link to comment https://forums.phpfreaks.com/topic/35921-javascript-div-hiding-toggle-not-toggling/#findComment-171181 Share on other sites More sharing options...
DaveLinger Posted January 28, 2007 Author Share Posted January 28, 2007 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 disappeardocument.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 https://forums.phpfreaks.com/topic/35921-javascript-div-hiding-toggle-not-toggling/#findComment-171221 Share on other sites More sharing options...
mainewoods Posted January 28, 2007 Share Posted January 28, 2007 [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 disappearif (document.getElementById('whichLayer').style.display == 'block') document.getElementById('whichLayer').style.display = 'none';}[/code] Link to comment https://forums.phpfreaks.com/topic/35921-javascript-div-hiding-toggle-not-toggling/#findComment-171409 Share on other sites More sharing options...
DaveLinger Posted January 28, 2007 Author Share Posted January 28, 2007 [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 disappearif (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 https://forums.phpfreaks.com/topic/35921-javascript-div-hiding-toggle-not-toggling/#findComment-171436 Share on other sites More sharing options...
Zeon Posted January 29, 2007 Share Posted January 29, 2007 remove the quotes around [i]whichLayer[/i]:[code]document.getElementById(whichLayer)[/code] Link to comment https://forums.phpfreaks.com/topic/35921-javascript-div-hiding-toggle-not-toggling/#findComment-171583 Share on other sites More sharing options...
DaveLinger Posted January 29, 2007 Author Share Posted January 29, 2007 still nothing =/ Link to comment https://forums.phpfreaks.com/topic/35921-javascript-div-hiding-toggle-not-toggling/#findComment-171894 Share on other sites More sharing options...
makeshift_theory Posted January 29, 2007 Share Posted January 29, 2007 Try this[code]function toggle(id){var layer = document.getElementById(id);if(layer.style.display == 'none')layer.style.display = 'block';elselayer.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 https://forums.phpfreaks.com/topic/35921-javascript-div-hiding-toggle-not-toggling/#findComment-172008 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.