PDA

View Full Version : eval this string..."imgOn" + [num-1] + ".src"


skeatt
11-28-2002, 05:32 PM
I've change how I want my function to work but don't understand the eval function, How can I eval this string....

"imgOn" + [num-1] + ".src"

to this.... imgOn[2].src

I've tried everything; this is what I've tried but just get "imgOn0 is undefined"

here is a cut down version of what i'm doing

imgOn = new Array();
imgOn[2] = new Image();
imgOn[2].src = "on.gif";

var num=3
var param=imgOn

function nnnnn(imgName,param) {
document[imgName].src=eval(param+[num-1]+".src")
}

Where is the typo? :confused:

brothercake
11-28-2002, 05:39 PM
Eval is evil ... and anyway you don't need it


document[imgName].src= imgOn[num-1].src;


should work - but it doesn't take account of the "param" replacement; you can do that, but it's not as simple as

param = imgOn

you would need to copy the array.

skeatt
11-28-2002, 05:50 PM
brothercake I cracked it...:o just as you posted

document[imgName].src=eval(btnState + '[' + (num-1) + ']' + '.src');

I tried the ' ' "" bit before, but this time I put parenthesis around my (num-1)

and everything worked.:cool:

brothercake
11-28-2002, 09:25 PM
Yeah right - the parenthesis ensures that you get a calculation, rather than string manipulation.

cool

joh6nn
11-29-2002, 03:55 AM
yes, but you still don't need the eval.

document[imgName].src= btnState[num-1].src;

skeatt
12-02-2002, 04:43 AM
john6mm: Well I'll be! I can see that working, why do you see so many books then teaching how to make roll-overs using the eval method? (visual Quickstart Guide)

document[imgField].src=eval(newImg + ".src")

(not array like I'm playing with but similar)

Is there a reason for this in special circumstances.? like browser compatability or something:confused:


I went and tried your suggestion, but without the eval you get :
error 'src' is not an object