This seems like a good way to get the job done, but I'm not too sure. I implemented your code, with a few tweaks for variable names and such, and I'm having the same kind of problem. It works fine when there really is a selection, but when the cursor is just somewhere in the textarea with nothing actually selected it can't find the value of its position.
I thought maybe this would work:
info = document.getElementById("post"); // this refers to my textarea
msg = info.value;
var left = info.createTextRange();
sel refers to document.selection.createRange()... this was initialized in a previous function and testing shows that before this line runs sel has already been assigned
Also if I replace it with just document.selection it still has an error here
start = left.text.length + 1;
end = start + info.selection.text.length;
alert("Start = " + start); // this was just for testing
But, of course, it doesn't.
Do you see anything wrong with it???? I get an error at this line left.setEndPoint("EndToStart", sel);.
Does this make sense? What do you think?