SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Threaded View

  1. #1
    SitePoint Evangelist Alluvian's Avatar
    Join Date
    Jan 2007
    Location
    Orlando, FL
    Posts
    417
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    UPDATED (can mozilla reset a textarea scrollbar?) cursor position in a textarea

    I am using the following script that I got from these forums to insert BBcode into a text area with the ID of "writeup".

    Code:
    function ubbc(open, end){ 
        var tArea = document.myform.writeup; 
        var isIE = (document.all)? true : false; 
        var open = (open)? open : ""; 
        var end = (end)? end : ""; 
    
        if(isIE){ 
            tArea.focus(); 
            var curSelect = document.selection.createRange(); 
            if(arguments[2]){ 
                curSelect.text = open + arguments[2] + "]" + curSelect.text + end; 
            } else { 
                curSelect.text = open + curSelect.text + end; 
            } 
        } else if(!isIE && typeof tArea.selectionStart != "undefined"){ 
            var selStart = tArea.value.substr(0, tArea.selectionStart); 
            var selEnd = tArea.value.substr(tArea.selectionEnd, tArea.value.length); 
            var curSelection = tArea.value.replace(selStart, '').replace(selEnd, ''); 
            if(arguments[2]){ 
                tArea.value = selStart + open + arguments[2] + "]" + curSelection + end + selEnd; 
            } else { 
                tArea.value = selStart + open + curSelection + end + selEnd; 
            } 
        } else { 
            tArea.value += (arguments[2])? open + arguments[2] + "]" + end : open + end; 
        } 
    }
    The 'open', and 'end' arguments are the bbcode that goes before and after the selection. The code works GREAT, but it does not return the cursor to the position it started in after the insert. The code above leaves the button that was clicked to invoke it with the focus and when you click back into the text field, the cursor is back at the top.

    Is there a bit of code that could be added to reset the cursor position to a specific spot, maybe the beginning or end of the starting selection text?
    Last edited by Alluvian; May 15, 2007 at 07:41.


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
  •