title: Auto-Update table cell based on user input in Javascript
body: <p>I'm trying to auto-update a table cell based on changes in another table cell. The user has a firstname, lastname and username. The username consists of first initial + lastname. My current code is:</p>
  
  <pre><code>&lt;html&gt;
    &lt;head&gt;
      &lt;script type = "text/javascript"&gt;
        function updateUsername(){
          var firstname = document.getElementById("tdFirstname").firstChild.firstChild.nodeValue;
          var lastname = document.getElementById("tdLastname").firstChild.firstChild.nodeValue;
          var username = document.getElementById("tdUsername").innerHTML = firstname + lastname;
        }
      &lt;/script&gt;
    &lt;/head&gt;
    &lt;body&gt;
      &lt;table id = 'confirm'&gt;
        &lt;tr&gt;
          &lt;th&gt;Firstname&lt;/th&gt;
          &lt;td id = 'tdFirstname'&gt;
            &lt;div onkeypress = 'updateUsername()' contentEditable&gt;
              Johnny
            &lt;/div&gt;
          &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
          &lt;th&gt;Lastname&lt;/th&gt;
          &lt;td id = 'tdLastname'&gt;
            &lt;div onkeypress = 'updateUsername()' contentEditable&gt;
              Sppleseed
            &lt;/div&gt;
          &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
          &lt;th&gt;Username&lt;/th&gt;
          &lt;td id = 'tdUsername'&gt;
            JSPPLESEED
          &lt;/td&gt;
        &lt;/tr&gt;
      &lt;/table&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>The code works (just putting the first and last names together, not worred about first initial yet), but its one letter behind. It doesn't update after a new character is entered, meaning if I update lastname to Appleseed (like it should be), just changing the one letter has no effect (username remains Sppleseed), but when a second key is pressed, it would change to Appleseed. Is it something wrong with my code? or did I miss something that I should have to update after the new character has been entered / deleted. This is very frustrating.</p>