CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    TextArea Selection

    Hello guys!

    I know I can use .select() to select all text, but what I want to do is:

    I have a TextArea. In that, the user highlights a word (it can be any word), then he clicks on a button to add a <B> tag in front of the selected word, and a </B> tag after the selected word.

    How can I get the user's selected word in this case ¿

    Any help will be greatly appreciated.

  2. #2
    Join Date
    Feb 2005
    Location
    Denmark
    Posts
    742

    Re: TextArea Selection

    I've not dabbled much into this, so I don't know if it is this you need, but...


    FireFox there are two javascript functions called "selectionStart" and "selectionEnd" which will give you the indexes of the respective start and end of a selection.

    In Internet Explorer I only know if using "document.selection.createRange().text;" which seems to work.


    Hope this helps

  3. #3
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: TextArea Selection

    Hello Alsvha!
    Thanx for replying!
    You see, I'm accustomed to have a SelectionStart and SelectionEnd type functions in VB and other programming languages, so that was the first things I searched for.
    It's the very first time I hear about document.selection.createRange().text;, I'll do some research on it and will let you know asap.

    Thanx once again!

  4. #4
    Join Date
    Feb 2005
    Location
    Denmark
    Posts
    742

    Re: TextArea Selection

    There are likely other proberties on document.selection.createRange() because it returns an object.
    As said though - I've not looked much into these in the past, so I can't guide you much.

    However, this seems to be the entry point on microsofts pages:
    http://msdn.microsoft.com/workshop/a..._selection.asp

  5. #5
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: TextArea Selection

    Hello again Alsvha, you have helped me a lot!!
    All I needed was document.selection.createrange.

    I (you) have fixed my problem, and just for interest sake, I'm supplying the code here.
    The purpose of this script is to add <b> and </b> tags around a selection in the TextArea.
    PHP Code:
    <html>
    <
    head>
    <
    title>Add Formatting tags!</title>

    <
    script type="text/javascript">
    function 
    makeBold() {
       
    theSelection document.selection.createRange().text;
       if (!
    theSelection)
         return 
    false;
       if (
    document.selection.createRange().parentElement().tagName != 'TEXTAREA')
         return 
    false;
       
    document.selection.createRange().text "<b>" theSelection "</b>";
    }
    </
    script>



    </
    head>
    <
    body>

    <
    textarea rows 10 cols 50></textarea>

    <
    input type Button value "Click" onClick="makeBold()">

    </
    body>
    </
    html
    Works like a charm!!
    Thanx once again!

  6. #6
    Join Date
    Feb 2005
    Location
    Denmark
    Posts
    742

  7. #7
    Join Date
    Nov 2006
    Posts
    1

    Talking Re: TextArea Selection

    Oh this worked a treat. You sir are a star.

    I had been looking for this feature for 2 days or more, searching under every word I could think of.

    Now here's a tricky one, how could this be adapted to URLs. Say you select the text, then click a url button, a pop up box apears with a space to enter the url, then you hit Ok or Submit and it adds the code/tags to the box.

    Could that be done?

    Oh and I only registered to to say how wicked that code is!

  8. #8
    Join Date
    May 2002
    Posts
    10,943

    Re: TextArea Selection

    Welcome to the forums laughingbuddha. Please remember that when you have a new question to post it in a new thread.

    If you are talking about typing a URL into a popup box and then going to that URL, just using prompt() and then window.location, or location.replace().

    If you want to get the selection from the URL line in the browser, that cannot be done.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured