Jump to content

insert into textarea at cursor position

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
webcodez

webcodez

    Programmer

  • Members
  • PipPipPipPip
  • 149 posts
Hi there,

As I'm not quite a javascript programmer I've been searching for a script/function to insert text into a textarea at cursor position but don't seem to get it working. This is the script ( the javascript function I just found on some website ) :

<html>
<head>
<script>
//modified version of http://www.webmaster...orum91/4686.htm
//myField accepts an object reference, myValue accepts the text string to add
function insertAtCursor(myField, myValue) {
 //fixed scroll position
 textAreaScrollPosition = myField.scrollTop;
    //IE support
    if (document.selection) {
        myField.focus();
        //in effect we are creating a text range with zero
        //length at the cursor location and replacing it
        //with myValue
        sel = document.selection.createRange();
        sel.text = myValue;
    //Mozilla/Firefox/Netscape 7+ support
    } else if (myField.selectionStart || myField.selectionStart == '0') {
        myField.focus();
        //Here we get the start and end points of the
        //selection. Then we create substrings up to the
        //start of the selection and from the end point
        //of the selection to the end of the field value.
        //Then we concatenate the first substring, myValue,
        //and the second substring to get the new value.
        var startPos = myField.selectionStart;
        var endPos = myField.selectionEnd;
        myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length);
        myField.setSelectionRange(endPos+myValue.length, endPos+myValue.length);
    } else {
        myField.value += myValue;
    }
 //fixed scroll position
 myField.scrollTop = textAreaScrollPosition;

}
</script>
</head>
<body>
<a href="#" onClick="insertAtCursor('test', 'texttest')">add 'texttest'</a>
<textarea name='test' id='test'></textarea>
</body>
</html>

But doesn't work... Do I use the function in wrong way? Or how should I get this to work?

Would also be interested to know how to put text before position of a selection inside a textarea ( and after the selection ).

Any help would be much appreciated,

Thanks in advanced.
Webcodez.

#2
webcodez

webcodez

    Programmer

  • Members
  • PipPipPipPip
  • 149 posts
Ehh, stupid me :D

Thought the function automaticly accessed the object but ofcourse I had to give the object itself and not the name/id (still dunno which is used o.o) of the textarea as it also requires a form.

Fixed it:

<html>
<head>
<script>
//modified version of http://www.webmaster...orum91/4686.htm
//myField accepts an object reference, myValue accepts the text string to add
function insertAtCursor(myField, myValue) {
 //fixed scroll position
 textAreaScrollPosition = myField.scrollTop;
    //IE support
    if (document.selection) {
        myField.focus();
        //in effect we are creating a text range with zero
        //length at the cursor location and replacing it
        //with myValue
        sel = document.selection.createRange();
        sel.text = myValue;
    //Mozilla/Firefox/Netscape 7+ support
    } else if (myField.selectionStart || myField.selectionStart == '0') {
        myField.focus();
        //Here we get the start and end points of the
        //selection. Then we create substrings up to the
        //start of the selection and from the end point
        //of the selection to the end of the field value.
        //Then we concatenate the first substring, myValue,
        //and the second substring to get the new value.
        var startPos = myField.selectionStart;
        var endPos = myField.selectionEnd;
        myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length);
        myField.setSelectionRange(endPos+myValue.length, endPos+myValue.length);
    } else {
        myField.value += myValue;
    }
 //fixed scroll position
 myField.scrollTop = textAreaScrollPosition;
 
}
</script>
</head>
<body>
<a href="#" onClick="insertAtCursor(document.myform.test, 'texttest')">add 'texttest'</a>
<form name='myform' id='myform'>
<textarea name='test' id='test'></textarea>
</form>
</body>
</html>