Note that there are some explanatory texts on larger screens.

plurals
  1. POhtml 5 counter on drop function
    primarykey
    data
    text
    <p>i am trying to make some sort of puzzle. I have red this tutorial: <a href="http://www.html5rocks.com/tutorials/dnd/basics/" rel="nofollow">http://www.html5rocks.com/tutorials/dnd/basics/</a> changed text div to picture and everything is working perfectly, until point, where i want to add counter on moves. With text, everything is working well, but if i have image, count number is overwriting my image i tried with </p> <pre><code>document.getElementById("some-new-div").innerHTML+= 'moves: ' + newCount; </code></pre> <p>but it is not working</p> <pre><code> this.handleDrop = function(e) { // this/e.target is current target element. if (e.stopPropagation) { e.stopPropagation(); // stops the browser from redirecting. } // Don't do anything if we're dropping on the same column we're dragging. if (dragSrcEl_ != this) { dragSrcEl_.innerHTML = this.innerHTML; this.innerHTML = e.dataTransfer.getData('text/html'); // Set number of times the column has been moved. &lt;----------------------------------------------- var count = this.querySelector('.count'); var newCount = parseInt(count.getAttribute('data-col-moves')) + 1; count.setAttribute('data-col-moves', newCount); count.textContent = 'moves: ' + newCount; //document.getElementById("count1").innerHTML+= 'moves: ' + newCount; } return false; }; </code></pre> <p>i will post whole html document i hope it is ok.</p> <pre><code>&lt;!DOCTYPE HTML&gt; &lt;html&gt; &lt;head&gt; &lt;link rel="stylesheet" type="text/css" href="styl.css"&gt; &lt;title&gt;&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="columns-full"&gt; &lt;table&gt; &lt;tr&gt;&lt;td&gt;&lt;div class="column"&gt;&lt;div class="count" data-col-moves="0"&gt;&lt;img src='img/img1.jpg' alt='img1.jpg' /&gt;&lt;/div&gt;&lt;/div&gt;&lt;/td&gt;&lt;td&gt;&lt;div class="column"&gt;&lt;img src='img/img2.jpg' alt='img2.jpg' /&gt;&lt;/div&gt;&lt;/td&gt;&lt;td&gt;&lt;div class="column"&gt;&lt;img src='img/img3.jpg' alt='img3.jpg' /&gt;&lt;/div&gt;&lt;/td&gt;&lt;td&gt;&lt;div class="column"&gt;&lt;img src='img/img4.jpg' alt='img4.jpg' /&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt; &lt;/table&gt; &lt;/div&gt; &lt;div class="count1" data-col-moves="0"&gt;&lt;/div&gt; &lt;/body&gt; &lt;/html&gt; &lt;script&gt; Element.prototype.hasClassName = function(name) { return new RegExp("(?:^|\\s+)" + name + "(?:\\s+|$)").test(this.className); }; Element.prototype.addClassName = function(name) { if (!this.hasClassName(name)) { this.className = this.className ? [this.className, name].join(' ') : name; } }; Element.prototype.removeClassName = function(name) { if (this.hasClassName(name)) { var c = this.className; this.className = c.replace(new RegExp("(?:^|\\s+)" + name + "(?:\\s+|$)", "g"), ""); } }; var samples = samples || {}; // Full example (function() { var id_ = 'columns-full'; var cols_ = document.querySelectorAll('#' + id_ + ' .column'); var dragSrcEl_ = null; this.handleDragStart = function(e) { e.dataTransfer.effectAllowed = 'move'; e.dataTransfer.setData('text/html', this.innerHTML); dragSrcEl_ = this; // this/e.target is the source node. this.addClassName('moving'); }; this.handleDragOver = function(e) { if (e.preventDefault) { e.preventDefault(); // Allows us to drop. } e.dataTransfer.dropEffect = 'move'; return false; }; this.handleDragEnter = function(e) { this.addClassName('over'); }; this.handleDragLeave = function(e) { // this/e.target is previous target element. this.removeClassName('over'); }; this.handleDrop = function(e) { // this/e.target is current target element. if (e.stopPropagation) { e.stopPropagation(); // stops the browser from redirecting. } // Don't do anything if we're dropping on the same column we're dragging. if (dragSrcEl_ != this) { dragSrcEl_.innerHTML = this.innerHTML; this.innerHTML = e.dataTransfer.getData('text/html'); // Set number of times the column has been moved. &lt;----------------------------------------------- var count = this.querySelector('.count'); var newCount = parseInt(count.getAttribute('data-col-moves')) + 1; count.setAttribute('data-col-moves', newCount); count.textContent = 'moves: ' + newCount; //document.getElementById("count1").innerHTML+= 'moves: ' + newCount; } return false; }; this.handleDragEnd = function(e) { // this/e.target is the source node. [].forEach.call(cols_, function (col) { col.removeClassName('over'); col.removeClassName('moving'); }); }; [].forEach.call(cols_, function (col) { col.setAttribute('draggable', 'true'); // Enable columns to be draggable. col.addEventListener('dragstart', this.handleDragStart, false); col.addEventListener('dragenter', this.handleDragEnter, false); col.addEventListener('dragover', this.handleDragOver, false); col.addEventListener('dragleave', this.handleDragLeave, false); col.addEventListener('drop', this.handleDrop, false); col.addEventListener('dragend', this.handleDragEnd, false); }); })(); &lt;/script&gt; </code></pre> <p>thanks and sorry for my bad english</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload