www.webdeveloper.com
Results 1 to 2 of 2

Thread: write text at the cursor

  1. #1
    Join Date
    Jun 2009
    Posts
    20

    write text at the cursor

    Hi,

    Using an onMouseDown, I can determine the coordinates of the cursor. Is there any way to write some text (a string) at that point. I am using at the moment a <textareaa> tag. I want the user to be able to click in the text and at a string to be inserted at that point.

    Thanks for your help.
    Andynic

  2. #2
    Join Date
    Jun 2009
    Posts
    20
    I found this code posted by JohnKrutsch at http://www.codingforums.com/showthread.php?t=57217
    that seems to do what I want:

    <html>
    <head>

    <script type="text/javascript">
    <!--
    function insert(elt,txtToIns) {
    if (elt.setSelectionRange){
    elt.value = elt.value.substring(0,elt.selectionStart) + txtToIns +
    elt.value.substring(elt.selectionStart,elt.selectionEnd) +
    elt.value.substring(elt.selectionEnd,elt.value.length);
    }
    else if (document.selection && document.selection.createRange) {
    elt.focus();
    var range = document.selection.createRange();
    range.text = txtToIns + range.text;
    }
    }
    //-->
    </script>
    </head>

    <body>

    <form>
    <input type="button" value="hello" onclick="insert(this.form.ta,' hello ')">
    <input type="button" value="my freind" onclick="insert(this.form.ta,' my friend ')">
    <br />
    <textarea rows="7" cols="30" name="ta">
    This is sample text, click anywhere in here then
    choose on of the buttons above to see text inserted.
    </textarea>
    </form>
    </body>
    </html>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles