I'm trying to copy text and then trying to add it in the place where the cursor currently is.
Basically i'm trying to add the text from the right hand side to the WordPress tinymce editor, where the cursor is.
Please see the image here for reference
I tried using the below code
function insertTextIntoFocusedTextFieldInOpener(text) {
var field = window.opener.document.activeElement;
if (field.tagName == "TEXTAREA" || (field.tagName == "INPUT" && field.type == "text" ) {
insertAtCursor(field, text);
}
}
which calls the below function and sends the text and field.
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;
}
}
But it doesn't insert the text when i click the ADD button.
Could someone please guide a better alternative solution?
This one line below did the job.
Full code for Onclick ADD button: