javascript - passing variables

They have: 330 posts

Joined: Apr 2000

I need to pass variables between javascript functions and am having some small problems. Here is an example of what I want to do...

function closeSub(subID){
subID.style.display = 'none';
}

function delayCloseSub(subID){

setTimeout('closeSub(subID)',1000);

}
'

the error states that subID is undefined. if I call closeSub directly it works.

what am I doing wrong?

Thanks for your help.

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

Try this:

function closeSub(subID){
subID.style.display = 'none';
}

function delayCloseSub(subID){

setTimeout('closeSub(' + subID + ')',1000);

}
'

?

Smiling

They have: 330 posts

Joined: Apr 2000

Still generating errors. Here is a replica of what isn't working.

<html>
<head>
&lt;script language='javascript'&gt;
function displaySub(subID){
subID.style.display = '';
}

function closeSub(subID){
alert(subID);
subID.style.display = 'none';
}

function delayCloseSub(subID){
setTimeout('closeSub(' + subID + ')',1000);
}
&lt;/script&gt;
</head>
<body>
<div id='testID1' style='display: none'>
test
</div>

<a href='#' onMouseOver='displaySub(testID1)' onMouseOut='delayCloseSub(testID1)'>hover here</a>
</body>
</html>
'
Thanks for your help

CptAwesome's picture

He has: 370 posts

Joined: Dec 2004

since testID1 is an object, you have to call one of it's values, not the object itself to refer to it, kind of crazy huh? Point is, the following code will work:

function closeSub(subID.id){
subID.style.display = 'none';
}

function delayCloseSub(subID){

setTimeout('closeSub(' +subID.id + ')',1000);

}
'

(full example)

<html>
<head>
&lt;script language='javascript'&gt;
function displaySub(subID){
subID.style.display = '';
}

function closeSub(subID){
alert(subID.id);
subID.style.display = 'none';
}

function delayCloseSub(subID){
setTimeout('closeSub(' + subID.id + ')',10);
}
&lt;/script&gt;
</head>
<body>


<a href='#' onMouseOver='displaySub(testID1)' onMouseOut='delayCloseSub(testID1)'>hover here</a>


<div id='testID1' style='display: none'>
test
</div>
</body>
</html>
'

They have: 330 posts

Joined: Apr 2000

Thank you very much. You would die laughing if you knew how much code I wrote to get around the .id problem I had.

66 lines of javascript code
7 functions

I think I'll just ad .id back and make it work properly.

Thanks again.

CptAwesome's picture

He has: 370 posts

Joined: Dec 2004

No worries, adding .id was just a guess that I tried that worked, I tried .name first, heh.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.