Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8803355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:19:11+00:00 2026-06-14T01:19:11+00:00

I have a little problem with HTML5 drag&drop API. Let’s write the code so

  • 0

I have a little problem with HTML5 drag&drop API.
Let’s write the code so everyone can understand.

<div id="draggerContainer" droppable="true">
  <div class="draggableItem" draggable="true" data-id="001">[... Stuff inside like <img> and other <div> ...] </div>
  <div class="draggableItem" draggable="true" data-id="002">[... Stuff inside like <img> and other <div> ...] </div>
  <div class="draggableItem" draggable="true" data-id="003">[... Stuff inside like <img> and other <div> ...] </div>
  <div class="draggableItem" draggable="true" data-id="004">[... Stuff inside like <img> and other <div> ...] </div>
  <div class="draggableItem" draggable="true" data-id="005">[... Stuff inside like <img> and other <div> ...] </div>
  <div class="draggableItem" draggable="true" data-id="006">[... Stuff inside like <img> and other <div> ...] </div>
</div>

Well that’s is awkard but I don’t know how to use the drag&drop api. I’ve already tried the tutorial on html5rocks.com but nothing.. Can someone tell me how to start and create a sorting system in html5 and Js (better jQuery if is possible) ?

That’s the source code from the tutorial.

function handleDragStart(e) {
  // Target (this) element is the source node.
  this.style.opacity = '0.4';
  dragSrcEl = this;
  e.dataTransfer.effectAllowed = 'move';
  e.dataTransfer.setData('text/html', this.innerHTML);
}
function handleDragOver(e) {
  if (e.preventDefault) {
    e.preventDefault(); // Necessary. Allows us to drop.
  }
  e.dataTransfer.dropEffect = 'move';  // See the section on the DataTransfer object.
  return false;
}
function handleDragEnter(e) {
  // this / e.target is the current hover target.
  this.classList.add('over');
}
function handleDragLeave(e) {
  this.classList.remove('over');  // this / e.target is previous target element.
}
function handleDrop(e) {
  // this / e.target is current target element.
  if (e.stopPropagation) {
    e.stopPropagation(); // stops the browser from redirecting.
  }
  // See the section on the DataTransfer object.
  return false;
}
function handleDragEnd(e) {
  // this/e.target is the source node.
  [].forEach.call(cols, function (col) {
    col.classList.remove('over');
  });
}
var cols = document.querySelectorAll('#draggerContainer .draggableItem');
[].forEach.call(cols, function(col) {
  col.addEventListener('dragstart', handleDragStart, false);
  col.addEventListener('dragenter', handleDragEnter, false)
  col.addEventListener('dragover', handleDragOver, false);
  col.addEventListener('dragleave', handleDragLeave, false);
  col.addEventListener('drop', handleDrop, false);
  col.addEventListener('dragend', handleDragEnd, false);
});

I’ve used Javascript scripts used in tutorial on HTML5Rocks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T01:19:13+00:00Added an answer on June 14, 2026 at 1:19 am

    http://jsfiddle.net/WxTqd/

    First, when a drag starts, send the data-id attribute as drag information, so that you know which element is being dragged:

    $("#draggerContainer")
        .on("dragstart", ".draggableItem", function(e) {
            e.dataTransfer.setData("id", $(this).data("id"));
        })
    

    You have to preventDefault() on dragover to get drag working:

    .on("dragover", ".draggableItem", function(e) {
        e.preventDefault();
    })
    

    And on drop, you have to swap elements:

    .on("drop", ".draggableItem", function(e) {
        // get the element being dragged according to drag information
        var id = e.dataTransfer.getData("id");
    
        var $draggingElem = $(".draggableItem").filter(function() {
            return $(this).data("id") === id;
        });
    
    
        // This is to make sure the element appears under the cursor
        var indexDrag = $draggingElem.index();
        var indexThis = $(this).index();
    
        if(indexDrag < indexThis) {
            $draggingElem.insertAfter(this);
        } else if(indexDrag > indexThis) {
            $draggingElem.insertBefore(this);
        }
    });
    

    This uses e.dataTransfer, which jQuery doesn’t expose by default. To enable this, execute this once per page load:

    $.event.props.push("dataTransfer");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a little problem. This is my code: stackPanelShapesContainer = new StackPanel(); InitializeStackPanle();
i have a little problem with my java socket code. I'm writing an android
I have a little problem with Codemirror ( http://codemirror.net/manual.html ) My code: $(document).ready(function ust()
I have a little problem and I'm hopping that you can help me solve
Hey guys, I have a little problem with a simple ajax request. I can't
I have a little problem with my if(isset($_POST['submit'])) code. What I want is some
i have a little problem with the Google Feed API and jquery each. The
I have a little problem showing html file as documentation on my google code
I have a little problem with one code. The html: <div id="navigation"> <ul> <li><a
I have a little problem with the following code. When I click Send it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.