View Full Version : 'Eval' method Question?

08-22-2002, 06:34 PM
In a form i have 'n' fields and i call the information of fields dynamic, that is, in a function a call the fields values width eval and it works.

var teste = eval("document.frm." + d1 + ".value")

The problem is, i want to change the field value and the 'Eval' method doesn't works. The code that i have is the next one:

eval("document.frm." + d1 + ".value") = tes

the 'd1' is the name of field on form and 'tes' is a value for example '333'

when a execute this function is visible a error 'Cannot assign to a function result'

The question is? Exists another way to put in a field dynamic information???

Pedro Américo

08-22-2002, 06:55 PM
Well, I believe you need to do those operations separately
var formObj = eval("document.frm."+d1);
formObj.value = tes; However, you can also use the elements collection of the form, and skip the eval() altogether
document.frm.elements[d1].value = tes; Note that this assumes that d1 is variable holding the name of the form field. The elements collection can also be referenced literally. If the form field's id or name was acutally "d1" then our code would look like this
document.frm.elements['d1'].value = tes;

08-22-2002, 07:37 PM
If you're doing an assignment with eval(), the assignment (the = and the RH operand) must be part of the string being eval()uated as well:

<script type="text/javascript" language="javascript">

var tes = '333'
function demo(field_name) {
eval("document.frm." + field_name + ".value = " + tes);

onload = function() {demo('d1')};

<form name="frm">
<input type="text" name="d1">