Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Fix HTML errors
Source Link
loganfsmyth
  • 162.3k
  • 31
  • 349
  • 259

You want a basic loop to convert and add each item.

I have also cleaned up your HTML a ton. You didn't have any proper closing tags. I have also changed all of the 'name' attributes to 'id' attributes so that 'getElementById' would work properly, which I missed on my first pass.

<html>
  <head>
    <script type='text/javascript'>
      function sum(){ 
        var val = document.getElementById('userInput').value;
        var temp = val.split(" ");
 
        var total = 0;
        var v;
        for(var i = 0; i < temp.length; i++) {
          v = parseFloat(temp[i]);
          if (!isNaN(v)) total += v; 
        } 
        document.getElementById('resultSum''resultSumValue').innerHTMLvalue = total; 
      } 
    </script>
  </head>
  <body>
    <form id="input">
      <textarea id="userInput" rows=20 cols=20></textarea> 
      <input id="Run" type=Button value="run" onClick="sum()" />
    </form>

    <form id="resultSum">
      <input id="resultSumValue" type="text" />
    </form>
  </body>
</html>

This will also ignore any values that are 'NaN' (Not a Number).

If you want the numbers to only be integers (no decimals), change parseFloat to parseInt.

You want a basic loop to convert and add each item.

function sum(){
  var val = document.getElementById('userInput').value;
  var temp = val.split(" ");
 
  var total = 0;
  var v;
  for(var i = 0; i < temp.length; i++) {
    v = parseFloat(temp[i]);
    if (!isNaN(v)) total += v;
  }
  document.getElementById('resultSum').innerHTML = total;
}

This will also ignore any values that are 'NaN' (Not a Number).

If you want the numbers to only be integers (no decimals), change parseFloat to parseInt.

You want a basic loop to convert and add each item.

I have also cleaned up your HTML a ton. You didn't have any proper closing tags. I have also changed all of the 'name' attributes to 'id' attributes so that 'getElementById' would work properly, which I missed on my first pass.

<html>
  <head>
    <script type='text/javascript'>
      function sum(){ 
        var val = document.getElementById('userInput').value;
        var temp = val.split(" ");
        var total = 0;
        var v;
        for(var i = 0; i < temp.length; i++) {
          v = parseFloat(temp[i]);
          if (!isNaN(v)) total += v; 
        } 
        document.getElementById('resultSumValue').value = total; 
      } 
    </script>
  </head>
  <body>
    <form id="input">
      <textarea id="userInput" rows=20 cols=20></textarea> 
      <input id="Run" type=Button value="run" onClick="sum()" />
    </form>

    <form id="resultSum">
      <input id="resultSumValue" type="text" />
    </form>
  </body>
</html>

This will also ignore any values that are 'NaN' (Not a Number).

If you want the numbers to only be integers (no decimals), change parseFloat to parseInt.

Updated to specify function wrapping.
Source Link
loganfsmyth
  • 162.3k
  • 31
  • 349
  • 259

You want a basic loop to convert and add each item.

varfunction sum(){
  var val = 0,document.getElementById('userInput').value;
  var temp = val.split(" ");

  var total = 0;
  var v;
  for(var i = 0; i < temp.length; i++) {
    v = parseFloat(temp[i]);
    if (!isNaN(v)) sumtotal += v;
  }
  document.getElementById('resultSum').innerHTML = sum;total;
}

This will also ignore any values that are 'NaN' (Not a Number).

If you want the numbers to only be integers (no decimals), change parseFloat to parseInt.

You want a basic loop to convert and add each item.

var sum = 0, v;
for(var i = 0; i < temp.length; i++) {
  v = parseFloat(temp[i]);
  if (!isNaN(v)) sum += v;
}
document.getElementById('resultSum').innerHTML = sum;

This will also ignore any values that are 'NaN' (Not a Number).

If you want the numbers to only be integers (no decimals), change parseFloat to parseInt.

You want a basic loop to convert and add each item.

function sum(){
  var val = document.getElementById('userInput').value;
  var temp = val.split(" ");

  var total = 0;
  var v;
  for(var i = 0; i < temp.length; i++) {
    v = parseFloat(temp[i]);
    if (!isNaN(v)) total += v;
  }
  document.getElementById('resultSum').innerHTML = total;
}

This will also ignore any values that are 'NaN' (Not a Number).

If you want the numbers to only be integers (no decimals), change parseFloat to parseInt.

Source Link
loganfsmyth
  • 162.3k
  • 31
  • 349
  • 259

You want a basic loop to convert and add each item.

var sum = 0, v;
for(var i = 0; i < temp.length; i++) {
  v = parseFloat(temp[i]);
  if (!isNaN(v)) sum += v;
}
document.getElementById('resultSum').innerHTML = sum;

This will also ignore any values that are 'NaN' (Not a Number).

If you want the numbers to only be integers (no decimals), change parseFloat to parseInt.