Javascript: How can I use a variable name when updating a form value?
<p>Here is part of the function:</p>

<pre><code>function updateResponse(question, answer) {
    questionId = 'q'+question;
    document.forms[0].questionId.value = answer;
</code></pre>

<p>Calling the function with something like:</p>

<pre><code>&lt;td onClick='javascript:updateResponse(1, 1);'&gt;
</code></pre>

<p>The form element I want it to update looks something like:</p>

<pre><code>&lt;input type='hidden' name='q1' value='' /&gt;
</code></pre>

<p>Using Firefox web console I get:</p>

<pre><code>TypeError: document.forms[0].questionId is undefined
</code></pre>

<p>Thanks!</p>

<p>UPDATE: Found this after much googling and it works. Still seems like there may be a more graceful way to achieve this though?</p>

<pre><code>eval(\"document.forms[0].\"+questionId+\".value = answer;\");
</code></pre>
