With this tutorial i made simple drag and drop web app. But I cant do it with DOM. Here is my code jsfinddle . It is not working on jsfiddle but if u download it it will. The script should be placed behind the divs. When you uncoment <div class="column" draggable="true"><span>A</span></div>
it will work (not in jsfiddle). So how can i made it with DOM ?
Asked
Active
Viewed 601 times
0

tprieboj
- 1,680
- 6
- 31
- 54
-
are you sure you know what a DOM is? – nicholaswmin Apr 25 '15 at 10:59
-
i want to make HTML doc dynamically ... so i want to create it with DOM. Why are you asking ?? is something wrong with that init() function ? – tprieboj Apr 25 '15 at 11:03
-
Please be more specific about your problem – Chavdar Slavov Apr 25 '15 at 11:03
-
i want to apply those drag events on DOM created objects... – tprieboj Apr 25 '15 at 11:05
-
I think they are using DOM as a synonym for vanilla javascript. – Taplar Apr 25 '15 at 11:06
-
This is dom http://www.w3schools.com/js/js_htmldom_methods.asp – tprieboj Apr 25 '15 at 11:07
-
1I think the confusion is stemming from, any manipulation made to the page is made through the DOM. So saying you want to do it with DOM is not very clear. Saying you want to do it dynamically or something of that nature would be more descriptive of what your after. – Taplar Apr 25 '15 at 11:09
1 Answers
1
Going off the assumption you meant doing the draggable with dynamically created elements, I've updated your jsfiddle. http://jsfiddle.net/7c3v0s1s/6/ I wrapped the code in a namespace while doing the changes.
HTML
<div class="containter">
<div id="columns"></div>
</div>
Javascript
var localNameSpace = {
dragSrcEl: null
, bindDraggables: function() {
var cols = document.querySelectorAll('#columns .column');
[].forEach.call(cols, function(col) {
col.addEventListener('dragstart', localNameSpace.handleDragStart, false);
col.addEventListener('dragenter', localNameSpace.handleDragEnter, false);
col.addEventListener('dragover', localNameSpace.handleDragOver, false);
col.addEventListener('dragleave', localNameSpace.handleDragLeave, false);
col.addEventListener('drop', localNameSpace.handleDrop, false);
col.addEventListener('dragend', localNameSpace.handleDragEnd, false);
});
}
, createDraggables: function() {
var colDiv = document.getElementById('columns');
var divC = document.createElement('div');
var spanC = document.createElement('span');
divC.className = 'column';
divC.draggable = 'true';
spanC.innerHTML = 'A';
divC.appendChild(spanC);
colDiv.appendChild(divC);
}
, handleDrop: function(e) {
if(e.stopPropagation){
e.stopPropagation();
}
if(dragSrcEl != this){
localNameSpace.dragSrcEl.innerHTML = this.innerHTML;
this.innerHTML = e.dataTransfer.getData('text/html');
}
return false;
}
, handleDragEnd: function(e) {
var cols = document.querySelectorAll('#columns .column');
this.style.opacity = 1;
[].forEach.call(cols, function(col){
col.classList.remove('over');
});
}
, handleDragEnter: function(e) {
this.classList.add('over');
}
, handleDragLeave: function(e) {
this.classList.remove('over');
}
, handleDragOver: function(e) {
if(e.preventDefault){
e.preventDefault();
}
e.dataTransfer.dropEffect = 'move';
return false;
}
, handleDragStart: function(e) {
this.style.opacity = 0.4;
localNameSpace.dragSrcEl = this;
e.dataTransfer.effectAllowed = 'move';
e.dataTransfer.setData('text/html', this.innerHTML);
}
, init: function() {
var readyStateCheckInterval = setInterval(function() {
if (document.readyState === "complete") {
clearInterval(readyStateCheckInterval);
localNameSpace.createDraggables();
localNameSpace.bindDraggables();
}
}, 10);
}
};
localNameSpace.init();

Taplar
- 24,788
- 4
- 22
- 35
-
Reference to the checking ready state code block: http://stackoverflow.com/questions/978740/javascript-how-to-detect-if-document-has-loaded-ie-7-firefox-3 – Taplar Apr 25 '15 at 11:51
-
i added for loop to create more elemenst http://jsfiddle.net/7c3v0s1s/7/ but is says Uncaught ReferenceError: dragSrcEl is not defined – tprieboj Apr 25 '15 at 13:58
-
dragSrcEl, in the manner in which I used it above, belongs to localNameSpace. so to use it you would need to fully qualify it: localNameSpace.dragSrcEl – Taplar Apr 25 '15 at 19:56