Results 1 to 2 of 2

Thread: Focusing the Cursor

  1. #1
    Join Date
    May 2008
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Focusing the Cursor

    I have used "Anylink" to create a Smilies table for my Site's Blogs. It's fine, so this is NOT a Dynamicdrive query! The problem is that I have used this:

    function doCopyS(smilie) {
    myvalue=document.myform.mytext.value;
    value=myvalue+' '+smilie;
    document.myform.mytext.value=value;
    myform.mytext.focus();}

    "myform" and "mytext" refer to the textarea in which the user types the text for the blog.

    It almost works. The only thing is that it places the curor in front of the smilie coding (as in :lol instead of after it where I want it. Any ideas? If you want more info, I can zip the whole thing up, stick it into one of my many Downloaders, and then post the URL here.

    If you want to see it in action, go here (test site):

    http://mwob-test.dynalias.com/Smilies-table.html

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    First off, your function can be simplified:

    Code:
    function doCopyS(smilie) {
    document.myform.mytext.value += ' '+smilie;
    document.myform.mytext.focus();
    }
    After getting that, I found this using Google (search terms - move cursor textarea javascript):

    Code:
    function setCursorPosition(oInput,oStart,oEnd) {
       	       if( oInput.setSelectionRange ) {
        	         oInput.setSelectionRange(oStart,oEnd);
                 } 
                 else if( oInput.createTextRange ) {
                    var range = oInput.createTextRange();
                    range.collapse(true);
                    range.moveEnd('character',oEnd);
                    range.moveStart('character',oStart);
                    range.select();
                 }
           }
    at this address:

    http://www.faqts.com/knowledge_base/...html/aid/15728

    Put it all together:

    Code:
    function setCursorPosition(oInput,oStart,oEnd) {
       	       if( oInput.setSelectionRange ) {
        	         oInput.setSelectionRange(oStart,oEnd);
                 } 
                 else if( oInput.createTextRange ) {
                    var range = oInput.createTextRange();
                    range.collapse(true);
                    range.moveEnd('character',oEnd);
                    range.moveStart('character',oStart);
                    range.select();
                 }
           }
    
    function doCopyS(smilie) {
    document.myform.mytext.value += ' '+smilie;
    document.myform.mytext.focus();
    setCursorPosition(document.myform.mytext, document.myform.mytext.value.length, document.myform.mytext.value.length);
    }
    Works here.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

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