Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts

    Easily insert text at/surrounding selections

    Just a quick 10-minute thing I whipped up out of boredom:
    http://www.jasonkarldavis.com/textarea.html

    In Mozilla, every textarea now has:

    surroundSelection(leftstr, rightstr)

    and insertAtSelection(str)

    methods.

    In IE, you need to explicitly give textareas those methods by:

    ExtendTextArea(refToTextArea)

    And now that textarea has those same methods.


    I see this question asked enough that I thought it might be useful enough to post this.

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    1,137
    Thanks
    0
    Thanked 0 Times in 0 Posts
    nothing happens when you click the buttons in opera 6.0

    scroots
    Spammers next time you spam me consider the implications:
    (1) that you will be persuaded by me(in a legitimate mannor)
    (2)It is worthless to you, when i have finished

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    Zwolle, The Netherlands
    Posts
    1,120
    Thanks
    2
    Thanked 31 Times in 31 Posts
    when using the img button and there is no selection, the cursor just is between words, the img tag is inserted before the textarea. For the rest, very useful i think
    I am the luckiest man in the world

  • #4
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    Originally posted by scroots
    nothing happens when you click the buttons in opera 6.0

    scroots
    Even the Mozilla-method is proprietary. There is no current standard definition which specifies precisely how to obtain the position of the caret, and a user-selection.

    Roelf, I've gotten that a few times too, but it typically works for me in IE. I only get that when the caret is at the end of the text. Like I said, this was a quick 10-minute thing. Ultimately, the IE-method is very susceptible to screwing up the page if the method is called and the selection is elsewhere on the page other than the textarea. I could compare the parentElement.tagName against 'TEXTAREA' I suppose to prevent that from happening... but the real flaw is in IE's handling of a selection when clicking on a button.

  • #5
    Moderator
    Join Date
    May 2002
    Location
    Hayward, CA
    Posts
    1,453
    Thanks
    1
    Thanked 21 Times in 19 Posts
    Are you sure of that, jkd? The Range specification states a blinking caret may be treated as a selection with starting and ending points identical.
    "The first step to confirming there is a bug in someone else's work is confirming there are no bugs in your own."
    June 30, 2001
    author, Verbosio prototype XML Editor
    author, JavaScript Developer's Dictionary
    https://alexvincent.us/blog

  • #6
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    Originally posted by Alex Vincent
    Are you sure of that, jkd? The Range specification states a blinking caret may be treated as a selection with starting and ending points identical.
    Of course. But, as I was saying, there is no specification which deals with accessing user-selections, whether or not they consist of anything.

    In other words, there is no W3C-equivalent to window.getSelection().getRangeAt() or even window.getSelection()

  • #7
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for this code jkd, it's helped me with some stuff I'm working on.

    Note: to get the parentElement of a TextRange in IE, you need the parentElement method

    var range = document.selection.createRange();
    if ( range.parentElement() != this ) return;
    range.text = left + range.text + right;

    Works for me
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #8
    Regular Coder
    Join Date
    Nov 2002
    Posts
    672
    Thanks
    1
    Thanked 1 Time in 1 Post
    You've got to highlight text in order for the bold and italic stuff to work. It isn't html though. What is it, bb code? I have no knowledge on a 3rd party code. In fact if I did, I would make my signature be links, but how is that done when html is turned off in these forums?


  •  

    Posting Permissions

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