title: how to split a string with javascript - Separate first name and surname from textbox
body: <p>I'm using the following code to split up surname from first + middle names and place them in three seperate fields. One holds the surname, one holds the firstnames and one holds all of them but re-arranged as lastname, firstname(s)</p>
  
  <pre><code>function switchName(ele){
          var splitName = ele.value.split(" ");
          var surname = splitName[splitName.length-1]; //The last one
          var firstnames 
          for (var i=0; i &lt; splitName.length-1; i++){
                  firstnames += splitName[i] + " "
                  }
  
      document.getElementById("hdnSNFNS").value = surname + ", " + firstnames;
      document.getElementById("hdnEmployeeSurname").value = surname;
      document.getElementById("hdnEmployeeFirst").value = firstnames;
  </code></pre>
  
  <p>It works as is but I get this output for the firstnames</p>
  
  <blockquote>
    <p>Stone, undefinedJoss Helen</p>
  </blockquote>
  
  <p>I've played around with it for ages but cant stumble across the correct solution.</p>