Highlighting part of the text in a text box

Discussion in 'Javascript' started by Aria, Oct 12, 2005.

  1. Aria

    Aria Guest

    Hi all,

    I've been struggling with this for days. How can I use javascript to
    high light a part of the text in a textbox or a textarea?

    OR

    if the user highlights a part of the text in a textbox or a textarea,
    how can I say which part of the text is highlighted? (ie: at what
    character the highlight starts and at what character the highlight
    ends).

    thanks in advance.
    Aria, Oct 12, 2005
    #1
    1. Advertising

  2. Aria wrote:
    > if the user highlights a part of the text in a textbox or a textarea,
    > how can I say which part of the text is highlighted? (ie: at what
    > character the highlight starts and at what character the highlight
    > ends).
    >


    function sel(o) {
    if (o.selectionStart) {
    return o.value.substring(o.selectionStart,o.selectionEnd);
    } else {
    return document.selection.createRange().duplicate().text;
    }
    }

    ....
    <input type="text" onmouseup="alert(sel(this))" />


    JW
    Janwillem Borleffs, Oct 12, 2005
    #2
    1. Advertising

  3. Aria

    Aria Guest

    thanks. Is o.selectionStart and o.selectionEnd the code to use for
    netscape?
    Aria, Oct 13, 2005
    #3
  4. Aria wrote:
    > thanks. Is o.selectionStart and o.selectionEnd the code to use for
    > netscape?
    >


    Yes.


    JW
    Janwillem Borleffs, Oct 13, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alan Cantor

    Code Text Highlighting Question

    Alan Cantor, Oct 7, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    458
    Alan Cantor
    Oct 7, 2003
  2. tshad
    Replies:
    3
    Views:
    319
    gh0st54
    Oct 29, 2004
  3. Gnarlodious
    Replies:
    4
    Views:
    17,527
    elizas
    May 5, 2010
  4. Jack
    Replies:
    8
    Views:
    257
  5. James Black
    Replies:
    2
    Views:
    115
    James Black
    Jul 26, 2007
Loading...

Share This Page