Q:

Update textbox values in Javascript every second

I'm trying to translate a pay and savings calculator I made in excel to a web page I can host on my website and access anywhere.
What I want is to have the values automatically update without the need of pressing a load of buttons.
I've done a bit of research and put together this code, but its not working...
window.setInterval(function () {
  var week1 = document.getElementsByName("week1").value;
  var week2 = document.getElementsByName("week2").value;
  var week3 = document.getElementsByName("week3").value;
  var week4 = document.getElementsByName("week4").value;
  var week5 = document.getElementsByName("week5").value;

  var weekTotal = week1 + week2 + week3 + week4 + week5;

  document.getElementById("totalHours").innerHTML = weekTotal;
}, 1000);

Any ideas?

A:

use element IDs, not names - then you can use document.getElementById()
cache those element variables so you don't have to call document.getElementById() every time
convert .value into a number with parseInt(s, 10) - although you haven't said how it fails this will be causing you a problem because .value is normally a string and using + on strings does concatenation, not addition.
don't do it in a timer loop - it'll prevent updates from happening in real time.  Instead, use an onchange (and/or oninput) event handler which will be called immediately whenever any value is updated.  With onchange the sum would be calculated each time the user moves from one input to another, with oninput it'll recalculate on each key press (or copy/paste, etc).

See http://jsfiddle.net/alnitak/7Gzs6/
var form = document.getElementById('myform');
var inputs = form.getElementsByTagName('input');
var totalView = document.getElementById('total');

function add(ev) {
    var total = 0;
    for (var i = 0, n = inputs.length; i < n; ++i) {
        var v = parseInt(inputs[i].value, 10);
        if (!isNaN(v)) {
            total += v;
        }
    }
    totalView.innerHTML = total;
}

form.addEventListener('change', add, false);​

