The BBCode editor allows you to select specific text from a textarea, have that text inserted into an Input field, and then add a URL string back to the textarea.
This code can be used on forum-style websites, making it easy to add URLs to your visitors' posts.
Live Lessons Example - JavaScript Insert text from 2 inputs into a TextArea at the cursors position

Origins
  1. September 02, 2010 - Originally posted and shared with the Coding Source Community.
  2. June 20, 2012 - Updated form elements and code
  3. October 31, 2022 - Updated with selected text in Textarea.
    Original code source to Selected text (javascriptkit.com«)

    To work with an [Input] field instead of DIV, code provided by: Michel Plungjan«
  4. Wednesday, February 3, 2026 10:32 PM EST - I had to change the document.myform.txta -to- document.myform.code

    It used to work with the name, but now it only works with the ID. Strange.
    CFFCS | CarrzSynEdit: | HTML (Hyper Text Markup Language)
    <input type="button" class="input" value="Add Text" 
    onclick="insertAtCursor(document.myform.code, 
    document.myform.txtb.value, document.myform.txtc.value)">

  5. When viewing the Code Example. Please follow the instructions located at the top of the Code Editor.

    For the Insertion to work, you will first need to
    Click inside this area, and press a key (Spacebar is the preferred method)
    This will activate the Form and make it work.
  6. Updated: May 30, 2026 - Changed from window.parent.insertAtCursor to insertAtCursor
    The reason for it is that I received the following error. [window.parent.insertAtCursor is not a function]
    Removing the window.parent from it resolved the issue.