...

View Full Version : I'm sure there is a really simple solution to this ...



stevenmarsh
07-11-2011, 01:58 PM
but I just can't figure it out. I have trawled the net for a solution and tried many different things that I have seen but still no luck. Maybe the answer is staring me right in the face and I just can't see it.

The code it executing the first time but as the setTimeout tries to start the second loop I get an error 'box is not defined'.

Any suggestions and explanations of what I am doing wrong much appreciated! :)



function expand(box) {

var box = document.getElementById(box);
var originalHeight = parseInt(box.style.height);
var inc = 5;

newHeight = (originalHeight + inc);
box.style.height = newHeight + "px";

int = setTimeout('expand(box)',100); }

bullant
07-11-2011, 02:01 PM
try


int = setTimeout(function(){expand(box);},100);

stevenmarsh
07-11-2011, 02:10 PM
Thank you bullant. It has done something but not solved the problem. The error I am now getting is 'box is null' instead.

stevenmarsh
07-11-2011, 02:21 PM
oops, just figured it out, pretty silly of me really. Thank you for pointing me in the right direction :). This is how it had to be changed:



function expand(box) {

var id = box;
var box = document.getElementById(id);
var originalHeight = parseInt(box.style.height);
var inc = 5;

newHeight = (originalHeight + inc);
box.style.height = newHeight + "px";

int = setTimeout(function(){expand(id);},100); }

bullant
07-11-2011, 02:21 PM
ok, I've had a closer look.


var box = document.getElementById(box);The value of box, an id (string) of some value, being passed to the function the first time is being overwritten by the value of box (which is now an object reference) after it is passed to getElementById()

Use a different variable name for the object reference to the element with the id = box.

stevenmarsh
07-11-2011, 02:23 PM
Thank you :)

bullant
07-11-2011, 02:23 PM
yep, no problem :) - good to see you sorted it out.

The two variables in red need to have different names.


looking at the time stamps, we are posting at near enough the same time ...lol :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum