/    Sign up×
Community /Pin to ProfileBookmark

function sends object instead of string

Okay, it might be because I’m feeling a little sick, or it might be that this genuinely makes no sense:

I’m trying to make a simple function that moves (animates) a div downwards at a constant speed over a distance.

Both the distance and the div (via id) should be passed to the function so that it can be used with any div on the site and so it’s easily usable in the future.

The code is as follows:

[code=php]
function drop_div(wDiv, pixels)
{
//alert(“hello world”);
drop_cont(wDiv, pixels, 0);
}

function drop_cont(wDiv, pixels, cur_pixels)
{
alert(wDiv);
if(pixels > cur_pixels)
{
cur_pixels += 1;
document.getElementById(wDiv).style.top += 1;
setTimeout ( “drop_cont(” + wDiv + “, ” + pixels + “, ” + cur_pixels + “)”, 10 );
}
}
[/code]

There are two functions just to be neat (no need to send the function 0 the first time for the third parameter).

The issue happens the first time drop_cont calls itself. Errors suggested it had something to do with wDiv being undefined or pointing at nothing the second time through so i threw the alert in to trace it. I expected it to give me a null, instead it returned the placeholder for an actual object ([object HTMLDivElement]).

So what I’m assuming is that somehow my String is getting automatically associated with the Div object it is the ID for. I have no idea why or how exactly to fix it. I’ve tried using temporary variables (mirroring the sent ones) so I can typecast and whatnot but it’s all for naught.

Any help would be GREATLY appreciated, thanks in advance.

-S

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@scragarOct 21.2008 — I've got 2 solutions, no 1 is to drop the document.getElementByID and just pass the object:
[code=php]
function drop_div(wDiv, pixels)
{
//alert("hello world");
drop_cont(document.getElementById(wDiv), pixels, 0);
}

function drop_cont(wDiv, pixels, cur_pixels)
{
alert(wDiv);
if(pixels > cur_pixels)
{
cur_pixels += 1;
wDiv.style.top += 1;
setTimeout ((function(i,j,k){
return function(){
drop_cont(i, j, k);
};
})(wDiv, pixels, cur_pixels), 10 );
}
}
[/code]

and the alternative is to include quotes around your element name so it doesn't get converted to an element in some strange way:
[code=php]
function drop_div(wDiv, pixels)
{
//alert("hello world");
drop_cont(wDiv, pixels, 0);
}

function drop_cont(wDiv, pixels, cur_pixels)
{
alert(wDiv);
if(pixels > cur_pixels)
{
cur_pixels += 1;
document.getElementById(wDiv).style.top += 1;
setTimeout ( "drop_cont('" + wDiv + "', " + pixels + ", " + cur_pixels + ")", 10 );
}
}
[/code]


I recommend the first one for speed, and the second one for compatibility with your current code, use whichever you wish.
Copy linkTweet thisAlerts:
@BlueGeekauthorOct 21.2008 — speed isn't an issue, compatibility is.

Thanks, I'll throw this in as soon as I get home. Wish me luck.
Copy linkTweet thisAlerts:
@BlueGeekauthorOct 21.2008 — edited out, double post
×

Success!

Help @BlueGeek spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...