Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ContentEditable DIV - disabling drag and drop

Is it possible to disable the drag-and-drop functionality on elements which have the contentEditable attribute set to true.

I have the following HTML page.

<!DOCTYPE html>
<html><meta charset="utf-8"><head><title>ContentEditable</title></head>
<body>
    <div contenteditable="true">This is editable content</div>
    <span>This is not editable content</span>
    <img src="bookmark.png" title="Click to do foo" onclick= "foo()">
    </span>
</body>
</html>

The main problem I'm facing is that it is possible to drag and drop the image into the DIV and it gets copied (along with the title and the click handler)

like image 358
sonofdelphi Avatar asked Jun 13 '10 17:06

sonofdelphi


People also ask

How do I turn off Contenteditable in HTML?

Just set contentEditable="false" . See this answer.

What is Contenteditable div?

contenteditable is an HTML attribute that you can add to any HTML element. If its value is true or an empty string, that means that the user can edit it by clicking the element. For example: <div contenteditable="true"> Change me! </ div>

How do I stop Enter key in Contenteditable?

This question already has answers here:$('p[contenteditable]'). keyup(function(e) { return e. which !== 13 });

What is false about Contenteditable attribute?

The contentEditable property of the HTMLElement interface specifies whether or not the element is editable. This enumerated attribute can have the following values: ' true ' indicates that the element is contenteditable . ' false ' indicates that the element cannot be edited.


2 Answers

The following snippet will prevent your div from receiving dragged content:

jQuery('div').bind('dragover drop', function(event){
    event.preventDefault();
    return false;
});

I (and several others) find this api to be strange and counter-intuitive, but it does prevent a contenteditable from receiving dragged content in all browsers except IE8 (including IE9 beta). As of yet, I cannot prevent IE8 from accepting contenteditable.

I'll update this answer if I find a way to do so.

like image 194
brad Avatar answered Sep 25 '22 00:09

brad


Interesting, I don't know the drag and drop interface well enough to be sure, but it looks like there's some sort of bug here. I modified your code slightly to add a handler for the drop event on the editable div:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>ContentEditable</title>
</head>
<body>
    <div contenteditable="true" ondrop="window.alert('dropped!');return false;">This is editable content</div>
    <span>This is not editable content</span>
    <span>
        <img src="bookmark.png" title="Click to do foo" onclick="foo()">
    </span>
</body>
</html>

I'm testing in Firefox 3.7 alpha: if I drag the image on to the middle of the text the event fires, I get an alert, and the return false stops the image being dropped. However if I drag on to the start of the text the event doesn't fire but the image gets inserted inside the div. Mind you, in Firefox 3.6 and Chromium 6.0 the event doesn't fire at all, so either I've completely misunderstood how it works or none of it works well enough right now to rely on.

like image 29
robertc Avatar answered Sep 25 '22 00:09

robertc