Is it possible to intercept and modify text that gets pasted into a textarea?
If intercepting isn't possible, can I modify it after being pasted? (Without modifying the already present text in the textarea.)
Is it possible to intercept and modify text that gets pasted into a textarea?
If intercepting isn't possible, can I modify it after being pasted? (Without modifying the already present text in the textarea.)
Maybe intercept keypress
es, to know when CTRL+C is pressed, cache current text, then at CTRL+C's keyup
, check current value against cached one, with simple text processing you can know the new text, and do whatever you want with, and update accordingly.
The best way I know of how to do this is to wait for the content to the text field to be pasted then wait for a keyup or a keydown trigger. This is shown in the code below:
<script language="javascript">
function testfunction()
{
// This function will execute whenever the content of
}
</script>
<textarea onkeyup="testfunction()" onkeydown="testfunction()"></textarea>
If you want to monitor a textarea for any changes, the following code would do that. It checks the value of the textarea every 1/10th of a second for any updates.
<textarea id="testfield"></textarea>
<script language="javascript">
var textarea = document.getElementById('testfield');
var textval = '';
function monitor()
{
if(textval != textarea.value)
{
textval = textarea.value;
testfunction();
}
setTimeout('monitor()', 100);
}
function testfunction()
{
// This function will get executed whenever the value of the text area is altered (at most within 1/10th of a second)
}
monitor();
</script>
In both cases, you can modify the value of the textarea when testfunction() then update the value of the textarea with the updated value.
With jQuery:
jQuery(function($){
$('#your_element').bind('paste', function(event){
event.preventDefault();
var clipboardData = event.originalEvent.clipboardData.getData('text/plain');
console.log(clipboardData);
});
}
});
Works in IE and Webkit. With Firefox you might have to use this:
http://intridea.com/2007/12/16/faking-onpaste-in-firefox?blog=company