title: Focus cursor after inserting text in textfield
body: <p>I made the following function to insert a message (msg) into a textfield. </p>
  
  <p>After inserting the text the cursor needs to be after the last character of the msg that was inputted. The textfield already contains some text.</p>
  
  <p>When I insert the message the cursor gets focussed somewhere near the end of the msg but certainly not after the last character. It seems like some characters don't get counted by .length? </p>
  
  <pre><code>     function insertAtCursor(msg) {
             var textArea = document.getElementsByName("message")[0];
             textArea.value = textArea.value.substr(0, textArea.selectionStart) + msg +        textArea.value.substr(textArea.selectionEnd);
             var endMsgPos = textArea.value.lastIndexOf(msg) + msg.length;
             textArea.setSelectionRange(endMsgPos, endMsgPos);
       }
  </code></pre>