4

I am trying to make a drag and drop feature using javascript and HTML5, no jQuery. And I just cannot see what is wrong. Been looking at this for a long time and can't see where i failed. Can someone find it?

    function doFirst(){
    mypic = document.getElementById('dragapple');
    mypic.addEventListener("dragstart", startDrag, false);

    leftbox = document.getElementById('leftbox');
    leftbox.addEventListener("dragenter", function(e){e.preventDefault}, false);
    leftbox.addEventListener("dragover", function(e){e.preventDefault}, false);
    leftbox.addEventListener("drop", dropped, false);
}
function startDrag(e){
    var code = '<img id="dragapple" src="stealeripsum.png">';
    e.dataTransfer.setData('Text', code);
}
function dropped(e){
    e.preventDefault();
    leftbox.innerHTML = e.dataTransfer.getData('Text');
}
window.addEventListener("load", doFirst, false);

Thanks

4
  • e.dataTransfer.gegData('Text');. gegData? Commented Feb 15, 2013 at 19:32
  • What does it do? Or not do? Commented Feb 15, 2013 at 19:37
  • with this you should be able to drag and drop a picture from 1 div to another. Commented Feb 15, 2013 at 19:40
  • 3
    dragapple only works on Mac ?? Commented Feb 15, 2013 at 19:43

4 Answers 4

3

It's always the little things that catch us in the end. Inside your definition for the "dragover" event listener, you need to provide an argument list () to e.PreventDefault:

leftbox.addEventListener("dragenter", function(e){e.preventDefault();}, false);
leftbox.addEventListener("dragover", function(e){e.preventDefault();}, false);

That way, the browser will stop the default event response (which is to dis-allow dropping) and let your drop operation complete. Here's my silly jsFiddle demo illustrating success. The blue square is the #dragapple.

Sign up to request clarification or add additional context in comments.

5 Comments

Finally! I'm been wracking my brain on this. Thanks for ending my misery. I knew the drop event wasn't firing but was at a loss as to why.
@KristianDaugaard It's the () parens at the end of preventDefault().
Not in the code you included. It just says e.preventDefault with no ().
oh THANK YOU!! all i needed was (); in my prevent default function! Finally!
LOL, it's technically not a syntax error. e.preventDefault returns the function itself, but then does nothing. It's a logic error.
3

.gegData('Text') or .getData('Text')?

function dropped(e){
   e.preventDefault();
   leftbox.innerHTML = e.dataTransfer.getData('Text');
}

5 Comments

Sorry that was a typo in the question. That is "getData" in the actual code and it doesn't work still :)
The image is draggable, it does not have the attribute, but that should not be needed.
img doesnt require draggable="true", its default
Well, what about appending the element to the target instead of setting it as the innerHtml? var data = e.dataTransfer.getData('Text'); e.target.appendChild(data); Like w3schools.com/html/html5_draganddrop.asp
i guess i could do that, it is just very frustrating. I have been following a video tutorial for this script, and it does simply not work for me, and i have checked and matched the code 4 times.
0

The html5 drag and drop api has a lot of bizarre edge cases. I just wrote a very general low-level api that makes drag-and-drop easy. Here's how you'd do it in your example:

function doFirst(){
    var mypic = document.getElementById('dragapple')
    dripDrop.drag(mypic, {
      image: true,
      start: function(setData) {
        setData('Text', '<img id="dragapple" src="stealeripsum.png">')            
      }
    })

    var leftbox = document.getElementById('leftbox')
    dripDrop.drag(leftbox, {
      drop: function(data) {
        leftbox.innerHTML = data.Text        
      }
    })
}

window.addEventListener("load", doFirst, false);

Check it out here: https://github.com/fresheneesz/drip-drop

Comments

-1

You need to make ondragover and ondrop events to the element that receives the dragged, and the dragged element need to be draggable="true".

ex:

<div ondragover="allowDrop(event);" ondrop="drop(event);">

function allowDrop(x) { x.preventDefault(); }

function drop(x) {  x.preventDefault(); var data=x.dataTransfer.getData("text"); ... }

4 Comments

a dragover is not needed for the drop effect. Javascripts drop effect and dragover effect are two different functions. dragover is mostly only for styling in my case and i haven't styled it yet. And about the draggable true, that is not the problem, since it is default.
@KristianDaugaard have you even tried? in my app the preventDefault is required to release the object.
I just tried removing my preventDefault()'s and to no help. I think i might have to go with the inline drop attrubute, although i was trying to avoid it.
@KristianDaugaard have you tried to change it from img to a simple text to see if it works?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.