Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I modify pasted text?

Tags:

html

jquery

dom

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.)

like image 776
Georg Schölly Avatar asked Sep 19 '25 18:09

Georg Schölly


2 Answers

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

like image 173
Carlos Avatar answered Sep 21 '25 11:09

Carlos


Maybe intercept keypresses, 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.

like image 41
aularon Avatar answered Sep 21 '25 11:09

aularon