Click to See Complete Forum and Search --> : insert text where text-marker is


michelle
10-29-2003, 06:14 AM
I have a text field in which I want to add some predefined text by clicking some links.
The problem I am having is to figure out how to add the text where the text marker is situated. The function I have now just adds it to the end.

I then started looking at execCommand, but there doesn't seem to any command for inserting plain text.
Is there such a thing?
http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/commandids.asp

<My Page>

<script>
function insertText(myText) {
var origContent = document.myForm.editor.value;
var newContent = origContent + myText;
document.myForm.editor.value = newContent;
}
</script>

<form action="" name="myForm" id="myForm">
<textarea cols="30" rows="5" name="editor" id="editor"></textarea>
</form>
<a href="#" onClick="insertText('this is a predefined text');">Predefined text 1</a><br>
<a href="#" onClick="insertText('this is another predefined text');">Predefined text 2</a>

</My Page>

Any thoughts?
// Michelle

SnowCrash
10-29-2003, 07:45 AM
You can do something like this (just a rough guideline):

Internet Explorer:
var Range = document.body.createTextRange();
Range.collapse();
Range.text = "your new text";

Mozilla:
var Selection = window.getSelection();
var Range = Selection.getRangeAt(0);
Range.startContainer.insertData();

Search google for "javascript + textRange" or "javascript getSelection" to find out more.

Hope that helps.
Snow Crash