title: jQuery - Copy text and add it in the place where the cursor currently is
body: <p>I'm trying to copy text and then trying to add it in the place where the cursor currently is.</p>
  <p>Basically i'm trying to add the text from the right hand side to the WordPress tinymce editor, where the cursor is.</p>
  <p><a href="https://i.stack.imgur.com/7Un6X.png" rel="nofollow noreferrer">Please see the image here for reference</a></p>
  <p>I tried using the below code</p>
  <pre><code>function insertTextIntoFocusedTextFieldInOpener(text) {
    var field = window.opener.document.activeElement;
    if (field.tagName == &quot;TEXTAREA&quot; || (field.tagName == &quot;INPUT&quot; &amp;&amp; field.type == &quot;text&quot; ) {
      insertAtCursor(field, text);
    }
  }
  </code></pre>
  <p>which calls the below function and sends the text and field.</p>
  <pre><code>function insertAtCursor(myField, myValue) {
        var doc = myField.ownerDocument;
        //IE support
        if (doc.selection) {
          myField.focus();
          sel = doc.selection.createRange();
          sel.text = myValue;
        }
        //FF, hopefully others
        else if (myField.selectionStart || myField.selectionStart == '0') {
          var startPos = myField.selectionStart;
          var endPos = myField.selectionEnd;
          myField.value = myField.value.substring(0, startPos) + 
                          myValue + myField.value.substring(endPos, myField.value.length);
        } 
        // fallback to appending it to the field
        else {
          myField.value += myValue;
        }
      }
  </code></pre>
  <p>But it doesn't insert the text when i click the <strong>ADD</strong> button.</p>
  <p>Could someone please guide a better alternative solution?</p>