Results 1 to 6 of 6

Thread: "Wrap" tags around selected text?!?!?

  1. #1
    Join Date
    Dec 2005
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default "Wrap" tags around selected text?!?!?

    Hi, I'm building new guestbook for my site. Is there any way to "wrap" text around selected text? For example:
    1. I have small textarea.

    1. Visitor writes words "Hello World" in textarea and selects it.

    1. Now visitor pushes button and value of textarea is now "<b>Hello World</b>".


    Is there any way to do that?

  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

    - John
    ________________________

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

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    There are easier ways... modify this.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Dec 2005
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    thanks!

  5. #5
    Join Date
    Dec 2005
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    but how can I add text to other side the selection?

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    I did say to modify it.
    Code:
    function wrapWith(fieldId, opening, closing) {
      var el = document.getElementById(fieldId);
      if(document.selection) {
        // I'm not sure how this would be done with IE, after looking at MSDN.
        // Maybe someone else knows.
      } else if(el.selectionStart || el.selectionStart == 0) {
        var begin = el.value.substring(0, el.selectionStart),
          end = el.value.substring(el.selectionStart + 1, el.selectionEnd),
          middle = el.value.substring(el.selectionEnd + 1),
          scroll = el.scrollTop;
        el.value = begin + opening + middle + closing + end;
        el.focus();
        el.selectionStart = begin.length;
        el.selectionEnd = (begin + opening + middle + closing).length;
        el.scrollTop = scroll;
      } else {
        el.value += opening + closing;
        el.focus();
      }
    }
    May need tweaking. Definitely needs IE support. MSDN is annoyingly cryptic, and Microsoft seem to have set out to build one of the most useless APIs they possibly could for this purpose...
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

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
  •