I have a jquery token tagit plugin and I want to bind to the paste event to add items correctly.
I'm able to bind to the paste event like so:
.bind("paste", paste_input)
...
function paste_input(e) {
console.log(e)
return false;
}
How can I obtain the actual pasted content value?
There is an onpaste event that works in modern day browsers. You can access the pasted data using the
getData
function on theclipboardData
object.Note that bind and unbind are deprecated as of jQuery 3. The preferred call is to on.
All modern day browsers support the Clipboard API.
See also: In Jquery How to handle paste?
How about this: http://jsfiddle.net/5bNx4/
Please use
.on
if you are using jq1.7 et al.Behaviour: When you type anything or
paste
anything on the 1st textarea the teaxtarea below captures the cahnge.Rest I hope it helps the cause.
:)
Helpful link =>
How do you handle oncut, oncopy, and onpaste in jQuery?
Catch paste input
code
It would appear as though this event has some
clipboardData
property attached to it (it may be nested within theoriginalEvent
property). TheclipboardData
contains an array of items and each one of those items has agetAsString()
function that you can call. This returns the string representation of what is in the item.Those items also have a
getAsFile()
function, as well as some others which are browser specific (e.g. in webkit browsers, there is awebkitGetAsEntry()
function).For my purposes, I needed the string value of what is being pasted. So, I did something similar to this:
You'll want to perform an iteration through the items, keeping a string concatenation result.
The fact that there is an array of items makes me think more work will need to be done, analyzing each item. You'll also want to do some null/value checks.
Another approach: That
input
event will catch also thepaste
event.You could compare the original value of the field and the changed value of the field and deduct the difference as the pasted value. This catches the pasted text correctly even if there is existing text in the field.
http://jsfiddle.net/6b7sK/