views:

299

answers:

5

I've been googling and trying this for a good while now, but comes nowhere. So here goes:

What I want to do is to drop text on a DIV tag and handle that with JavaScript. Something along these lines:

<script type="text/javascript">

function handleDrop(sender, args)
{
  $('#theDiv').html(args.textfromdrop);
}

</script>

<div id="theDiv" ondrop="handleDrop()" />

<br/>
<p>
This is some simple text. Draggable?
</p>

So, on this page I want to be able to drag contents from the paragraph for example to the div and it would handle the drop and change it's appearance accordingly (Or maybe just display that text, as long as it would handle it!). I've been trying with jQuery, but it seems to be a whole other model, and I can't set all my potential draggables as such because they should be able to come from everywhere. Is this even possible?

EDIT: Please correct me if I'm wrong, but these droppables all require a draggable to be dropped at it, right? What I would want is that you can drop text, pure text, from a page that you don't have any control of. This might sound weird, but it's for a firefox extension where you can drag content from a page to another page that resides in the side bar.

A: 

If you're really trying to do it with jQuery then this should help: SO jquery-droppables-change-element-on-drop

TStamper
A: 

I would recommend using an established Javascript Library such as jQuery or YUI.

Chris Ballance
A: 

Or if you prefer Prototype like I do: http://wiki.github.com/madrobby/scriptaculous/droppables

EDIT: Based on your revised question: No, there's no way to allow a user to drop text from one page to another page. Not unless you do decide to build a FireFox extension like you were saying. Even if you could find a way around the security issue where you cannot script a page that's not under the same domain, you can only drag and drop DOM elements within the window/iFrame they're in.

Josh
A: 

Have you considered creating a hidden textarea (ie with css style visibility:hidden) overlapping the div in question? Then check for drops with the onchange JavaScript event, or if that doesn't work, periodically the textarea's value for non-empty strings. I'm guessing your mileage will vary depending on the browser and operating system.

Marcello Bastea-Forte
This is a good idea. Will try it when I get home. The luxury is that I only need this to work in Firefox. All OSes though.
miccet
A: 

I have done this before and it CAN be done without any library with some effort.

I've built the following methods:

  1. Method that tracks your mouse movements.
  2. Method to read and pass the content when you drop.
  3. Used onmousemove and onclick events for the drag and drop methods.
  4. OnMouseOver for the div area where you'd like to drop the text - to detect whether the pointer is over the container (div) or not.
  5. Finally after dropping the text I deleted the original content (if needed) using innerHTML so it looks like it has been moved.

You can pretty much achieve a Windows like drag and drop functionality with this. I used it for drag and drop images, icons, etc.

If you need help with the coding I can give you some guidance, but most of it you will find if you Google around a little, then all you need to do is make them work together.

G Berdal