Graahf
Graahf

Reputation: 123

Insert text from popup into textarea

I'm trying to insert a input text from an popup to a textarea on the page, defined from cursor position.

The idea is, that a user clicks a button, a popup opens, where the user can write text in an input box and then click an button to insert that text to the textarea the cursor position was in.

position = null;

function cursorPosition () {
 if(!window.getSelection)
 {
  position = document.selection.createRange().duplicate();
 }
 else 
 {
  position = window.getSelection();
 }
}

function insertAtCaret (text)
{
 position.text = text;
}

In the popup window I have:

function onclose(text)
{
 var newtext= text;
 opener.insertAtCaret(newtext);
 window.close();
}

Can't make it work in chrome, only IE.... Everytime I get an

Uncaught TypeError: Property 'insertAtCaret' of object [object Window] is not a function

Any idea to make it work in all browsers?

Upvotes: 0

Views: 1868

Answers (2)

ameya rote
ameya rote

Reputation: 1114

Below code accepts input from an inputbox and adds it to the textarea as required. It appends the inputbox value to the textarea.

<!DOCTYPE html>
<html>
<body>

<p>Click the button to demonstrate the prompt box.</p>

<button onclick="myFunction()">Try it</button>

<textarea id="demo" cols="30" rows="5">
</textarea>

<script>
function myFunction()
{
var x;

var name=prompt("Please enter your name","Harry Potter");

if (name!=null)
  {
  x = document.getElementById("demo").value; 
  x = x + " Hello " + name + "! How are you today?"; // textarea value is appended here.
  document.getElementById("demo").value=x;
  }
}
</script>

</body>
</html>

Upvotes: 2

Dutts
Dutts

Reputation: 6191

I think there are issues with insertAtCaret in FF\Chrome, you might want to check out this instead.

Upvotes: 1

Related Questions