-3

I have a function like this:

function x(a,b,c) {
    eval("window.document.forms['"+ a +"']."+ b +".value = '"+ c +"'");
}

I understand why eval === evil most of the time, so what's the best way to write that function without using eval? Does the below makes sense? Why?

function x(a,b,c) {
    window.document.forms[a].b.value = c.toString();
}
Leandro Barbosa
  • 182
  • 3
  • 12

1 Answers1

1

Your above function has no reason to be using eval - use bracket notation to access an objects property via a variable:

window.document.forms[a][b].value = c.toString();
tymeJV
  • 103,943
  • 14
  • 161
  • 157