Results 1 to 7 of 7

Thread: Adding text surrounding highlighted text

  1. #1
    Join Date
    Jan 2009
    Posts
    82
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Adding text surrounding highlighted text

    How do you add particular text surrounding the text a user highlights.

    Similar to how it's done in this form like if someone highlights

    Code:
    text
    becomes ...

    Code:
    [tag]text[/tag]

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Code:
    function singleWrap(myField, myValue) {
     var obj = document.getElementById(myField);
     var start = obj.value.slice(0, obj.selectionStart);
     var end = obj.value.slice(obj.selectionEnd, obj.value.length);
     var text = start + myValue+' ' + end;
     document.getElementById(myField).value = text;
    }
    function wrapHighlighted(myField, myValue){
     var obj = document.getElementById(myField);
     var start = obj.value.slice(0, obj.selectionStart);
     var end = obj.value.slice(obj.selectionEnd, obj.value.length);
     var middle = obj.value.substring(obj.selectionStart, obj.selectionEnd);
     var text = start + '['+myValue+']' + middle + '[/'+myValue+'] ' + end;
     document.getElementById(myField).value = text;
    }
    The singleWrap function is used for inserting smileys.
    And the wrapHighlightedis used for wrapping 2 things around the highlighted.

    Heres the bold tag in your case:
    Code:
    <button type="button" onclick="wrapHighlighted('textarea','b')">B</button>
    And to make a smiley:
    Code:
    <button type="button" onclick="singleWrap('textarea',': )')">: )</button>
    Jeremy | jfein.net

  3. #3
    Join Date
    Jan 2009
    Posts
    82
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default

    This is great!

    Is there a way to highlight the text and remove the tags as well like an undo.

    Also, when you click the button, the button itself stays highlighted and selected. is there a way to remove the focus from the button after it's hit.

  4. #4
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Hmm for undo I don't know - maybe create a function that makes an array of every full textarea value... What do you mean the button stays highlighted?
    Jeremy | jfein.net

  5. #5
    Join Date
    Jan 2009
    Posts
    82
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default

    I mean the button has focus http://screencast.com/t/ZGNmZDFjZ

  6. #6
    Join Date
    Jan 2009
    Posts
    82
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default

    I found out today this code doesnt work in IE8 (not sure about previous versions.).

    I had mozilla in my home computer.

    In IE, when the function executes it basically highlights ALL text in the textfield (not what you highlight), copies and pastes it again and then surrounds it with the tags.

  7. #7
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Hmm - I don't know why. Try searching around google for something like this.
    Jeremy | jfein.net

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
  •