I made the following function to insert a message (msg) into a textfield. 

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.

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? 

```
     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);
     }

```