title: Loop through array and return sum of all values
body: <p>What I want to do is have numbers inputted by user and the sum of the numbers returned. My logic is as follows: </p>
  
  <ol>
  <li>User inputs string</li>
  <li>String is split to array</li>
  <li>Loop through array and sum all numbers</li>
  <li>Return sum</li>
  </ol>
  
  <p>And here is the code I have so far:</p>
  
  <pre><code>&lt;script type='text/javascript'&gt;
  
  var val=document.getElementById('userInput').value;
  var temp=val.split(" ");
  
  function sum() {
      for(var i=0, MISSING THIS BIT
  
      document.getElementById('resultSum').innerHTML=MISSING THIS BIT;
  }
  
  &lt;/script&gt;
  
  &lt;form name="input"&gt;
      &lt;textarea name="userInput" rows=20 cols=20&gt;&lt;/textarea&gt;
      &lt;input name="Run" type=Button value="run" onClick="sum()"&gt;
  &lt;form name="resultSum"&gt;&lt;input type=Text&gt;
  </code></pre>
  
  <p>I admit with humility that this is mostly probably wrong and appreciate anybody's time and effort.</p>
  
  <hr>
  
  <p>UPDATE: I have done as suggested and I get the following error on my code below:</p>
  
  <blockquote>
    <p>Message: 'document.getElementById(...)' is null or not an object Line:
    16 Char: 1 Code: 0</p>
  </blockquote>
  
  <pre><code>&lt;html&gt;
  
  &lt;script type='text/javascript'&gt;
  
  function sum(){
      var val = document.getElementById('userInput').value;
      var temp = val.split(" ");
  
      var total = 0;
      var v;
      for(var i = 0; i &lt; temp.length; i++) {
          v = parseFloat(temp[i]);
          if (!isNaN(v)) total += v;
      }
  
      document.getElementById('resultSum').innerHTML=total;
  
  }
  
  &lt;/script&gt;
  
  &lt;form name="input"&gt;
      &lt;textarea name="userInput" rows=20 cols=20&gt;&lt;/textarea&gt;
      &lt;input name="Run" type=Button value="run" onClick="sum()"&gt;
      &lt;form name="resultSum"&gt;&lt;input type=text&gt;
  &lt;html&gt;
  </code></pre>
  
  <p>Any suggestions? Thanks to all for being comprehensive - I have read both examples and understand the process now!</p>