0

I am trying to set values of dynamically generated fields using eval() function in javascript.Since I don't know the name of the field beforehand.

Here is what I have done.

here last two parameters are the name of the fields whose values I want to set. and the first two values are the values which I want to set them to.

function onRefreshTypes(fuelType,startType,fuelTypes,startTypes)
{

    var fuelTypes1=fuelTypes;
    var startTypes1=startTypes;


    eval("document.frm."+fuelTypes1+".value="+fuelType);//here is where I am getting error
    eval("document.frm."+startTypes1+".value="+startType);//here is where I am getting error
}

here frm is the name of the form in which the fields are generated I am getting an error "Unexpected identifier" where am I going wrong?

  • You should [avoid using eval()](http://stackoverflow.com/questions/197769/when-is-javascripts-eval-not-evil) where possible. You can use dynamic fields using the square bracket notation. `document.frm[fuelTypes1].value=` – Mike Causer Dec 24 '13 at 11:28
  • @MikeCauser isn't it exactly what I've suggested in my answer? – Pavlo Dec 24 '13 at 11:43
  • @Pavlo heh, it would appear so. I should really scroll down first :) – Mike Causer Dec 24 '13 at 11:45

1 Answers1

3

Don't use eval() here, use square brackets notation:

document.frm[fuelTypes1].value = fuelType;
Pavlo
  • 43,301
  • 14
  • 77
  • 113