...

View Full Version : Eval function assignment



NaveenB
11-23-2009, 06:42 PM
Hi All,

This is my function

function outputSelected() {

alert(window.opener.document.forms[0].txt11_19.value);
alert(window.opener.document.forms[0].rdo11.length);

var questionID = document.forms[0].cntQuestion.value;
var choiceID = document.forms[0].cntChoice.value;
var txtValue = document.forms[0].strEmpNo.value + " " + document.forms[0].strName.value;
alert(txtValue);
var leftValue = 'window.opener.document.forms[0].txt' + questionID + '_' + choiceID + '.value =' + txtValue;
eval(leftValue);
}

all the alerts work as expected. bu the eval function does not.
error: expected ';'

any ideas what is happening. or is there any other way of doing this.

Old Pedant
11-23-2009, 07:21 PM
Can I rewrite that, please?


function outputSelected()
{
var form = window.opener.document.forms[0];
alert( form.txt11_19.value );
alert( form.rdo11.length);

var questionID = form.cntQuestion.value;
var choiceID = form.cntChoice.value;
var txtValue = form.strEmpNo.value + " " + form.strName.value;
alert(txtValue);
form.elements["txt" + questionID + "_" + choiceID].value = txtValue;
}


FWIW, the error in your original is that when you do the eval you need quotes around the value being assigned, since it is clearly *NOT* a number.


var leftValue = 'window.opener.document.forms[0].txt' + questionID + '_' + choiceID + '.value = "' + txtValue + '"';

But isn't my way easier to read and understand?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum