0%

jQuery - Paste Event for Text Box

Demo: JsFiddle

HTML

1
2
3
<div>
<input type="text" id='txt'>
</div>

JavaScript

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$('#txt').on('paste', function(e){
var content = '';

if (isIE()) {
//IE allows to get the clipboard data of the window object.
content = window.clipboardData.getData('text');
} else {
//This works for Chrome and Firefox.
content = e.originalEvent.clipboardData.getData('text/plain');
}

alert(content);
});

function isIE(){
var ua = window.navigator.userAgent;

return ua.indexOf('MSIE ') > 0 || ua.indexOf('Trident/') > 0 || ua.indexOf('Edge/') > 0
}