title: How can I populate a table's cell through JavaScript code?
body: <p>I have a table as below. I have to populate the "Amount" field using the "Buy Quantity" and "Market Price" field. Amount = Buy Quantity*Market Price. I am doing something as -</p>
  
  <pre><code>&lt;script&gt;
  function populate() {
  var rows = document.getElementById("mytable").getElementsByTagName("tr");
  for ( var i = 1; i &lt;= rows.length; i++) {
      cells = rows[i].getElementsByTagName("td");
      for ( var j = 0; j &lt;= cells.length; j++) {
          if (j == 1) {
              var num1 =parseFloat(cells[1].childNodes[0].value);
              var num2 =parseFloat(cells[2].childNodes[0].data);
              var num3=num1 * num2;
              cells[3].childNodes[0].value = num3.toString();
          }
       }
     }
   }
  &lt;/script&gt;
  </code></pre>
  
  <p>I can get the values of column1 and column2, but the value in last column is not getting populated. The last line does not seem to work.</p>
  
  <pre><code>cells[3].childNodes[0].value = num3.toString();
  </code></pre>
  
  <p>What should I change?</p>
  
  <p><img src="https://i.stack.imgur.com/BfQMO.png" alt="myTable"></p>
  
  <p>The below html code is part of my .jsp file.</p>
  
  <pre><code> &lt;form action="BuyServlet"&gt;
      &lt;table border="1" cellpadding="5" id="mytable"&gt;
          &lt;tr&gt;
              &lt;th&gt;Stock Name&lt;/th&gt;
              &lt;th&gt;Buy Quantity&lt;/th&gt;
              &lt;th&gt;Market Price&lt;/th&gt;
              &lt;th&gt;Amount&lt;/th&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;Stock Name&lt;/td&gt;
              &lt;td&gt;&lt;input type="text" name="quantity" onblur="populate()"&gt;&lt;/td&gt;
              &lt;td&gt;122&lt;/td&gt;
              &lt;td&gt;&lt;input type="text" name="amount"&gt;
              &lt;/d&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;Stock Name&lt;/td&gt;
              &lt;td&gt;&lt;input type="text" name="quantity" onblur="populate()"&gt;&lt;/td&gt;
              &lt;td&gt;111&lt;/td&gt;
              &lt;td&gt;&lt;input type="text" name="amount"&gt;&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/table&gt;
  &lt;/form&gt;
  </code></pre>