SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Addict richard_h's Avatar
    Join Date
    May 2002
    Location
    London
    Posts
    301
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    textarea focus point

    I was wondering if there is a way to to detect where the cursor is currently focused in a textarea.

    I'm currently using this method to add HTML line breaks:

    document.forms[0].elements[2].value = document.forms[0].elements[2].value + '<br>';

    But this will obviously just add the BR tags at the end of the text. Thanks in advance.

  2. #2
    SitePoint Enthusiast
    Join Date
    Mar 2001
    Location
    northern Maine
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Gecko-based browsers have a:

    selectionStart
    and
    selectionEnd

    property of text inputs and textareas. They refer to the selection range in the box. If the selection is collapsed (i.e. a caret is blinking), both values are equal to each other and refer to the position of the caret in the text.

    It is much more complicated in IE revolving around some use of the nasty TextRange interface, a quick search of MSDN though for document.selection should answer your question for IE browsers.
    Jason - Contact Me
    Supermoderator @ CodingForums

  3. #3
    SitePoint Addict richard_h's Avatar
    Join Date
    May 2002
    Location
    London
    Posts
    301
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks I'll have a look.


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
  •