A Drag and Drop Planning Board with HTML5

Share this article

The Drag and Drop API is one of the new JavaScript APIs that let us add dynamic effects to our sites. There are two flavors of Drag & Drop: 1. Dragging elements into other elements, as defined in the Drag and Drop W3C spec. 2. Dragging files from your computer and into a webpage, in combination with the File API. In this article, we’ll be focusing on the former. To learn more about the latter, an excellent guide can be found at the Mozilla Developer Network. We’ll be building a simple agile planning board. We’ll have three categories: To Do, In Progress, and Done. We can move the items around on the board to mark their progress.

note: Planning Boards
The concept of a Planning Board or a Task Board is a part of the Scrum Agile software development methodology. To learn more, see: http://en.wikipedia.org/wiki/Scrum_(development)
To give you an idea of what we’re going to build, here’s a picture of the finished product. You can see the finished code on github: Drag and Drop Planning Board Let’s dive in, starting with the HTML!
<div id="board">
  <div id="todo">
    <div id="item1">
    <div id="item2">
  </div>
  <div id="inprogress"></div>
  <div id="done"></div>
</div>
Our first div, div id=”board” will act as the main container. The board will be divided up into three other divs. Finally, several smaller divs will act as note cards holding our items. The note cards will start in the “To Do” section. Here are the complete set of steps we’ll need to make our notecards draggable into the other sections of the bulletin board:
  1. Set the draggable attribute on our notecard div HTML elements.
  2. Add an Event Listener for the dragstart event on the notecards.
  3. Add an EventListener for the dragover event on all sections of our bulletin board.
  4. Add an EventListener for the drop event on all sections of our bulletin board.
First, we’ll add the draggable attribute to our two notecards.
<div id="board">
  <div id="todo">
    <div id="item1" draggable="true">
    <div id="item2" draggable="true">
  </div>
  <div id="inprogress"></div>
  <div id="done"></div>
</div>
Next, we need to specify the JavaScript code that will actually make the Drag and Drop magic happen. First, we’ll bind the dragstart event to our two notecards. We’ll do this via jQuery’s bind method, binding the dragstart event on either of our two notecards to the code inside an anonymous function :
$('#item1, #item2').bind('dragstart', function(event) {
  event.originalEvent.dataTransfer.setData("text/plain", event.target.getAttribute('id'));
});
In our anonymous function, we call the setData method, which is a method on the DataTransfer object. The DataTransfer object is one of the new objects outlined in the Drag and Drop API. This object allows us to store and retrieve data about the objects that are being dragged. The setData method takes two arguments:
  1. The drag data item kind. This describes what kind of data we’ll be storing
  2. The actual data we want to store. This is either a unicode or binary string.
The setData method adds the data we want to store about the dragged item to what the W3C spec calls the “drag data store“. Examining the line inside our anonymous function more closely, we can see that we’re calling setData on the dataTransfer object, and specifying both required arguments:
event.originalEvent.dataTransfer.setData("text/plain", event.target.getAttribute('id'));
We set our drag data item kind to be “text/plain”. And for the data itself, we’ll store the id of the notecard that we have begun to drag. By default, the dragged item is shown to be a ghost-like image of the original element. If you’d like to change the dragged image to a custom one, you can use dataTransfer’s setDragImage() method. Next, we need to make sure we can drag the notecards over onto the other sections of the bulletin board. To do so, we’ll bind the dragover event on the board sections. Again, we’ll do this via jQuery’s bind method:
$('#todo, #inprogress, #done').bind('dragover', function(event) {
  event.preventDefault();
});
This time, the code inside our anonymous function is considerably more simple. But what is it doing? By default, none of the all elements on the page will accept a dropped item. In order to override this default behavior, and enable the element to accept dragged items, we must pervent the default behavior from happening. We do this by calling the preventDefault() method on the event. For our last step, we need to actually do something once our notecards are dropped onto a new section of the bulletin board! As a first step, we’ll bind the drop event to the bulletin board sections, and define what to do once something is dropped onto them. Again, we do this by binding the bulletin board divs to an anonymous function, via jQuery’s bind method:
$('#todo, #inprogress, #done').bind('drop', function(event) {
    // do something!
});
We want to drop the whole div element for the dragged notecard into the new location. To do this, we first grab the notecard element’s id, which we previously stored to the dataTransfer object:
$('#todo, #inprogress, #done').bind('drop', function(event) {

  var notecard = event.originalEvent.dataTransfer.getData("text/plain");

});
Next, we’ll actually add a new element to the target, by using the appendChild method:
$('#todo, #inprogress, #done').bind('drop', function(event) {

  var notecard = event.originalEvent.dataTransfer.getData("text/plain");

  event.target.appendChild(document.getElementById(notecard));
});
Finally, as we did before, we’ll disable the default behavior that disallows any items to be dropped:
$('#todo, #inprogress, #done').bind('drop', function(event) {

  var notecard = event.originalEvent.dataTransfer.getData("text/plain");

  event.target.appendChild(document.getElementById(notecard));

  event.preventDefault();

});
Now, we can move our notecards around into any section we want! Drag and Drop Planning Board See the demonstration page. View source for the full code. To learn more about HTML5’s Drag and Drop, check out my forthcoming book, HTML5 and CSS3 for the Real World. You may also find the following sites useful:

Frequently Asked Questions (FAQs) about Drag and Drop Planning Board with HTML5

How can I make the drag and drop feature more interactive for users?

To make the drag and drop feature more interactive, you can add visual cues to indicate the draggable elements and their drop zones. For instance, you can change the cursor style to ‘move’ when hovering over a draggable element. You can also highlight the drop zones when a draggable element is being dragged. This can be achieved using CSS and JavaScript. Additionally, you can use the ‘dragstart’ and ‘dragend’ events to change the appearance of the draggable elements during the drag operation.

Can I use the HTML5 drag and drop API for touch devices?

The HTML5 drag and drop API does not support touch events natively. However, you can use third-party libraries like jQuery UI Touch Punch that extend the jQuery UI’s drag and drop functionalities to touch devices. Alternatively, you can use the Touch Events API to implement drag and drop for touch devices.

How can I store the state of the planning board?

You can store the state of the planning board using the Web Storage API. The ‘localStorage’ object can be used to store data without expiration, while the ‘sessionStorage’ object stores data for one session. You can save the state of the planning board whenever a drag and drop operation is completed. This way, the state of the planning board will be preserved even if the page is refreshed.

How can I make the planning board accessible?

To make the planning board accessible, you can use ARIA (Accessible Rich Internet Applications) attributes. The ‘aria-grabbed’ attribute can be used to indicate the draggable elements, and the ‘aria-dropeffect’ attribute can be used to indicate the drop zones. Additionally, you can provide keyboard shortcuts for the drag and drop operations.

Can I drag and drop multiple elements at once?

The HTML5 drag and drop API does not support dragging and dropping multiple elements at once natively. However, you can achieve this by using a workaround. When a drag operation starts, you can group the selected elements together and create a clone of this group. Then, you can drag the clone and drop it at the desired location.

How can I prevent certain elements from being dragged?

You can prevent certain elements from being dragged by not setting the ‘draggable’ attribute to ‘true’ for those elements. Alternatively, you can use the ‘dragstart’ event to cancel the drag operation based on certain conditions.

How can I customize the drag image?

You can customize the drag image by using the ‘setDragImage’ method of the ‘DataTransfer’ object in the ‘dragstart’ event. This method takes three arguments: the image element, and the x and y coordinates of the drag point relative to the image.

Can I use the HTML5 drag and drop API with frameworks like React or Angular?

Yes, you can use the HTML5 drag and drop API with frameworks like React or Angular. However, these frameworks have their own ways of handling events, which can cause conflicts with the drag and drop API. To avoid these conflicts, you can use third-party libraries like React DnD or Angular Drag and Drop that provide a more seamless integration.

How can I test the drag and drop feature?

You can test the drag and drop feature using automated testing tools like Selenium or Puppeteer. These tools provide methods to simulate the drag and drop operations. However, testing the drag and drop feature can be challenging due to its interactive nature.

How can I handle errors during the drag and drop operations?

You can handle errors during the drag and drop operations by using the ‘dragend’ event. This event is fired when a drag operation is being ended, either by dropping the element or by cancelling the operation. You can check the ‘DataTransfer’ object’s ‘dropEffect’ property in this event to determine whether the operation was successful or not. If it was not successful, you can display an error message or take other appropriate actions.

Alexis GoldsteinAlexis Goldstein
View Author

Alexis Goldstein first taught herself HTML while a high school student in the mid-1990s, and went on to get her degree in Computer Science from Columbia University. She runs her own software development and training company, aut faciam LLC. Before striking out on her own, Alexis spent seven years in Technology on Wall Street, where she worked in both the cash equity and equity derivative spaces at three major firms, and learned to love daily code reviews. She is a teacher and a co-organizer of Girl Develop It, and a very proud member of the NYC Resistor hackerspace in Brooklyn, NY.

drag and dropHTML5 Dev CenterHTML5 Tutorials & ArticlesjavascriptWeb Design Tutorials & Articles
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week