Placing text at cursorpoint
I know there are about a dozen posts similar to this one, but IŠve read them all, and none of them seem to give a straight answer.
So, what I want to do is add text to a textarea when a button is clicked. However, I don't just want it appended at the end, but inserted at the point where the cursor/caret currently is.
Having browsed the various posts above, IŠve put together the following:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
function insertText(elemID, txt)
var elem = document.getElementById(elemID);
if ( typeof elem.caretPos == 'undefined' ) // Opera 7 ??
elem.value += txt;
elem.caretPos.text = elem.caretPos.text.charAt(elem.caretPos.text.length - 1) == ' ' ? txt + ' ' : txt;
// elem.innerHTML += text;
<textarea id="txt1" cols=20 rows=20></textarea>
<input type="button" value="Insert some text" onclick="insertText('txt1', 'Hello');">
Thanks a whole bunch in advance !