...

View Full Version : eval function??



usban
08-26-2002, 04:56 PM
I think it is not a very big problem, so i think anyone colud help me. The problem is tha t i have a layer in my page. This layer contains a table with a lot of diferent colors, i want to show it in diferrent points of the page, each time for choose a color for one diferent thing.

The problem is when i'm trying to know which is the button that have make it visible. I have thought of a hidden field in which store the name of the object, but i don't know how to return the value to this object, i think it is possible with the eval function, but i don't understand how to use it. If someone can explain the way it works or can just tell me how i can solve the problem it wolud be great. THnk you :thumbsup:

beetle
08-26-2002, 05:31 PM
eval() is used to resolve string/variable concatenations into usable code. For example, lets say that I have three inputs
<input type="text" name="counter1">
<input type="text" name="counter2">
<input type="text" name="counter3">Now lets say I want a quickie function to increment these fields, but based on a passed index, not the passed field name. I'd use eval for this.
function incrementCounter(index) {
var obj = eval("document.forms[0].elements['counter"+index+"']");
obj.value += 1;
}

//Sample call
<span onClick="incrementCounter(2)">Add to counter 2</span>Does this explain it well enough?

joh6nn
08-26-2002, 07:15 PM
its usually best to avoid using eval if you can, and in this case, you don't need it.

function incrementCounter(index) {
var obj = document.forms[0].elements['counter'+index];
obj.value += 1;
}

beetle
08-26-2002, 07:37 PM
Thank you joh6nn. I know that, but usban was asking about eval(), so I made up a simple example to help explain it. I realize that in actual application, it's useless, but that's not its purpose, now is it?

Cheers :D

usban
08-27-2002, 09:46 AM
Thank you Both of you for your explanaition. Finally it was a silly a mistake what was giving me the trouble.

:thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum