views:

1776

answers:

5

All I need is to be able to detect when text is dropped into a Textarea. I then take that text and do stuff with it and clear the textarea. There may be many of these textareas and very high UI expectations, so polling is a last resort.

For IE, "onfocus" does the trick, since this event is fired after the user drops stuff into the textarea.

For Firefox, I can't find an event that works. I've tried onmouseup and onchange.. don't know what else to try from there. I'd hate to have to poll the textarea for content. Help much appreciated. Thanks.

Edit: For clarification, "dropped" means the user selects text (usually) from the page, but it doesn't matter where, drags it, and drops it into the textarea. This is not the same as Ctrl+V, or right click pasting (which take two different methods of detection, BTW), or (obviously) typing into the textarea. Specifically, it is the "drop" aspect of drag and drop. I really don't know how else to phrase it.

I feel this question was stated rather accurately. To humor me, assume that I meant any of the other operations on a textarea that you all have chosen to share. If I meant "pasting", don't you think I would have mentioned something about pasting in the title or content of my question? Same goes for typing in a textarea. But, perhaps, you all just don't know me well enough to know that I type what I mean, rather than mistakingly typing things only somewhat related to what I mean.

A: 

What do you mean by "dropped"? Are you talking about a paste (^V) operation? If so Aron's answer is indeed correct, you will get keydown/keyup (and the value will be readable in keyup).

A generic callback that will happen on any update to an input is 'onchange' - you could try that. Note that when typing, it doesn't get called until you finish and unfocus the input, so if you need instant updates on both typing and other forms of input, you still need 'onkeyup' as well.

Edit re clarification: IMO 'onchange' should be fired when a drag-and-drop causes content to be added to a text area; I consider it a browser bug that it is not (in Firefox at least). I can only concur in this case that polling is all you have left.

bobince
Thank you for the suggestion of "onchange." However, I already stated in my question that I've tried this. Perhaps you'd not be asking me "what do I mean?" if you would have read my question.
sam
Also, thank you for teaching me that "^ V" is a shortcut for pasting. I would have never figured that out otherwise. Luckily, I was born with the knowledge that the carrot character (^) stands for Ctrl. Phew.
sam
Apologies for being a bit too harsh. This is the best of the replies so far since you've noted that "onchange" only gets fired after an unfocus, and when a pasted value is readable (after keyup).
sam
A: 

Polling seems to be the only way to go.

When a Drag+Drop operation is started within the browser, it seems to cancel out everything else that's going on. You can witness this by putting "onmouseover" events on something on the page, then starting a DnD operation and dragging over it. The events will not fire.

It seems only a coincidence of how the browser internally handles "drag and drop" that it causes onfocus() to be fired for the textarea in IE and Safari. I doubt there is much defined in the W3 specification... not that it matters, W3 is crap, and besides nobody adheres to it completely.

In my question I stated the luxury of emptying out the textarea after each use, so I feel that polling for: textarea.value.length>0 is hardly any cost and shouldn't be much of a performance concern.

Thank you all for insulting me by assuming I'm a retard. If this had anything to do with "copying and pasting" it would be entirely true that I'm a retard for typing about "dropping." But, alas, it specifically has to do with dropping content into a textarea.

Down-mod away to sooth your pain.

sam
I'm not going to down vote you, but if you're going to come here and ask a question, phrase it oddly, and have people TRY TO HELP YOU, albeit on the wrong thing, there's no reason for you to be a dick. Seriously.
Paolo Bergantino
Have you read the responses? These people DIDN'T READ THE QUESTION. Then, they ask me: "are you sure that's what you meant? Dropping? Because I *know* about keydown, keyup, and stuff you already tried but that I didn't read in your question."
sam
I know, I know. But as a regular in this site, people that ask questions are more often than not, well, lost. They had good intentions, I can see where your frustration comes from, but there's no reason to be an ass to them when all they really wanted to do was help.
Paolo Bergantino
Well, I guess I'm being a bit too harsh. I certainly wouldn't be this much of a dick if it weren't for the magic of anonymity. On the other hand, just 4 minutes ago, Vincent Robert has suggested using mouseup and keyup events.
sam
Just try to remember these people (me included) are trying to help you. Even if the answer is incorrect, there is absolutely no need to be this ungrateful and rude. I hope you remember that asking your next question.
Aron Rotteveel
A: 

If you want your textarea to react to text being dropped, pasted or typed, your only solution would be a button. Even a timer polling the content will not help you.

You mention that you want the user to be able to type text in the textarea. And you mention that you want to clear the text whenever the event is fired. There is no possible way for the browser to know that the user has finished typing. You must rely on a specific action.

Now for the drop or paste, you can rely on the mouseup and keyup events in order to detect that.

Vincent Robert
This is surely a joke.
sam
+2  A: 

For Firefox, this works for me:

window.addEventHandler("dragdrop", function(event) {alert("Drop it like it's hot!")}, true)

Does not work in Safari, however. Haven't tried IE.

Chuck
Thank you! Nice to know there's a dragdrop event. I'm going with polling because it's browser agnostic and works well.
sam
A: 

Try 'ondragend' on IE and Firefox. I search something similar on Safari and Opera. Any suggestions ?

raphpell