PDA

View Full Version : variable doesn't seem to activate


hamdiggy
03-24-2003, 04:24 AM
i have a script that by all account should work. but it doesn't. and i don't know why. and it pisses me off.

http://www.hamiltondraws.com/javascriptsucks.php

there are three different ways i show of how the function COULD work. and even though the third does work, it's far too much code to be showing over and over again in a list of links. i would much rather be able to just call a function to do the if statement.

if someone could tell me why the first two links don't work properly, that would be great. cuz i am at a loss.

beetle
03-24-2003, 04:34 AM
This will work as long as you keep the style inline on the element. function toggleDisp( oId )
{
var o = document.getElementById( oId );
o.style.display = ( o.style.display == 'none' ) ? 'block' : 'none';
}The reason your first attempt didn't work is the browser was looking for an element with the name or id foldername, and not the string contained by foldername. In cases like these, you must you an element retrieval method, such as document.getElementById()

hamdiggy
03-24-2003, 04:57 AM
see, now i'm REALLY confused. cuz i tried doing the getelementbyid thing before and it didn't work. but this is working. thanks a bunch, man. here (http://www.hamiltondraws.com/?archive=archive) is where i'm using this, if you're interested. :)

Weirdan
03-24-2003, 08:37 AM
Or you can just eval string passed to function:

function folderCollapse(folderName) {
if(eval(folderName).style.display!='block')
eval(folderName).style.display='block';
else eval(folderName).style.display='none';
}

It works too.