Insert text in textarea at cursor position

Discussion in 'Javascript' started by cyber0ne, Apr 29, 2005.

  1. cyber0ne

    cyber0ne Guest

    I have a textarea for users to post stuff on a website, but right now
    it's just straight text and they have to add their own HTML tags for
    formatting. This usually isn't a problem except for carriage returns.
    Not being a Javascript guru by any means, I was looking up how to
    detect the keypress event, check to see if it's a carriage return
    (keycode 13, right?) and insert an HTML line break into the textarea.
    It all looks good except for one thing, I can only append the HTML to
    the end of the textarea. What if the user is going back and re-editing
    text? Any carriage returns in the middle of the textarea would result
    in an HTML line break at the end of the textarea.

    So, is there a way to insert text at the cursor position when a
    specific keyevent is detected? Does anyone have any example scripts
    that do this? Ideally I'd like it to be as cross-platform as possible,
    since I have about a 65:30:5::IE:Firefox:Other ratio of browsers that
    hit the site.


    Regards,
    David P. Donahue
    cyber0ne, Apr 29, 2005
    #1
    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. Matt Tapia

    Insert text at cursor position

    Matt Tapia, Nov 26, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    530
    Chris Taylor
    Nov 26, 2003
  2. Matt Tapia

    Insert text at cursor position...still

    Matt Tapia, Dec 1, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    583
    General Protection Fault
    Dec 1, 2003
  3. dmaziuk
    Replies:
    3
    Views:
    549
    Chris Gonnerman
    Jan 25, 2011
  4. Thomas

    insert text at cursor position

    Thomas, Jun 1, 2004, in forum: Javascript
    Replies:
    1
    Views:
    99
    Thomas 'PointedEars' Lahn
    Jun 13, 2004
  5. Michael
    Replies:
    2
    Views:
    239
    Michael
    Feb 17, 2006
Loading...

Share This Page