8

I see that when I try to read the value from a textarea field when its onpaste function is called, I get the old value of the field (the one before the paste operation), not the new value (the one after the paste operation).

Here is a demonstration of this behaviour: http://jsfiddle.net/qsDnr/

A copy of the code follows:

<!DOCTYPE html>
<html>
<head>
<title>On Paste</title>
<script type="text/javascript">
var textareaElement;
var previewElement;

function update()
{
    previewElement.innerHTML = textareaElement.value;
}

window.onload = function() {
    textareaElement = document.getElementById('textarea');
    previewElement = document.getElementById('preview');
    textareaElement.onpaste = update    
}
</script>
</head>
<body>
<textarea id="textarea">
</textarea>
<div id="preview">
</div>
</body>
</html>

You can confirm the behaviour with the following steps.

  1. Copy the string foo to your clipboard.
  2. Right-click on the textarea field and select 'Paste'. Nothing appears in the div element.
  3. Right-click on the textarea field and select 'Paste' again. foo appears in the div element.

Since I want the div element to always show what was updated in the textarea element with the paste operation, the desired output is foo and foo foo in step 2 and step 3 respectively.

One way I have managed to get the desired output is by delaying the update() function call with window.setTimeout(), so instead of

textareaElement.onpaste = update    

I have got

textareaElement.onpaste = function() {
    window.setTimeout(update, 100);
};

this time (demo: http://jsfiddle.net/cwpLS/). This does what I want. However, this feels more like a workaround rather than a straightforward way of doing what I want. I would like to know if there is any alternate or better way to do this.

Susam Pal
  • 27,101
  • 9
  • 71
  • 93
  • [This event](https://developer.mozilla.org/en/DOM/element.onpaste) is sent when the user *attempts* to paste text - can mean that onpaste is triggered before the text is pasted. – Joseph Mar 25 '12 at 04:02
  • `event.clipboardData.getData('text/plain')` allows to get content, would be pasted, but I'm not sure it is available in every browser and current text and selection should be taken into consideration as well. I'm afraid, you'll need to use timeout or track `onkeyup`/`onmouseup`. – kirilloid Mar 25 '12 at 04:07
  • @Joseph I agree. I am trying to figure if the problem I am trying to solve is best solved with `setTimeOut` like I have explained in this post or if there are better ways to solve this problem. – Susam Pal Mar 25 '12 at 04:08

2 Answers2

3

I'm pretty sure that you setTimeout solution is the only way to achieve the desired effect, or try to access the clipboard object - which can get messy if you're going for cross-browser & old browser support.

Eugene
  • 2,749
  • 2
  • 22
  • 30
0

There is no direct way to do it in cross browser. See the link below about the behaviour in firefox.

Mozilla

There is currently no DOM-only way to obtain the text being pasted; you'll have to use an nsIClipboard to get that information.

Also please have a look at the stackoverflow link which discuss about other possibilities.

Community
  • 1
  • 1
PraveenVenu
  • 7,825
  • 4
  • 28
  • 38