Click here to Skip to main content
13,089,504 members (64,779 online)
Click here to Skip to main content
Add your own
alternative version


21 bookmarked
Posted 15 Apr 2012

JavaScript example of drag and drop to a target

, 21 Apr 2012
Rate this:
Please Sign up or sign in to vote.
This article explains how to make drag and drop to a target work across multiple browsers.


This article is a sequel to the article I wrote earlier: Cross-browser drag and drop. In that article, I provided a JavaScript example of drag and drop. Here I would like to provide a JavaScript example of drag and drop to a target that would work in all browsers (including Safari-iPad).

This article is for those that like nuts and bolts and are not using third party libraries like jQuery. There is nothing wrong with using third party libraries – I just like more flexibility. For those that like nuts and bolts I would recommend this third party library: Redips.


Using the code

To use this script, use the SetupDragDrop JavaScript function. It will make some DIVs dragable and other DIVs drag targets based on the CSS class (Dragable or DropTarget) used. Please note that this example shows how to “capture” the element so that you don’t lose it if you move outside of the element or outside of the browser window. Here is the full JavaScript code:

var oDragTargets = [];
var oDragTarget = null;
var oDragItem = null;
var iClickOffsetX = 0;
var iClickOffsetY = 0;

function OnLoad(){

function SetupDragDrop(){
	oDragTargets = [];
	var oList = document.getElementsByTagName("div");
	for(var i=0; i<olist.length; y="e.clientY" x="e.clientX" otarget="oDragTargets[i];" i="0;" top="y;" left="x;" position="absolute" zindex="1000;" e="window.event;" (odragitem="=null)" iclickoffsety="e.targetTouches[0].pageY" iclickoffsetx="e.targetTouches[0].pageX" opos="GetObjPos(;" odragitem="o;" önmousedown="function(e){DragStart(oBox,e);return" obox.onmouseup="function(e){DragStop(oBox,e)};" obox.onmousemove="function(e){DragMove(oBox,e)};" obox.ontouchend="function(e){TouchEnd(e)};" obox.ontouchmove="function(e){TouchMove(e)};" obox.ontouchstart="function(e){TouchStart(e)};" (navigator.platform="="iPad"){" (o.classname="=" odragtargets[odragtargets.length]="GetObjPos(o);" o="oList[i];"> x && (oTarget.y + oTarget.h) > y){
			if (oDragTarget!=null && oDragTarget != oTarget.o) OnTargetOut();
			oDragTarget = oTarget.o;
	if (oDragTarget){
		oDragTarget = null;

function TouchMove(e){
    var x = e.targetTouches[0].pageX - iClickOffsetX;
    var y = e.targetTouches[0].pageY - iClickOffsetY;
    oDragItem = e.targetTouches[0].target;

function DragStop(o,e){
	if (o.releaseCapture){
	}else if (oDragItem){
		window.removeEventListener ("mousemove", DragMove2, true);
		window.removeEventListener ("mouseup",   DragStop2, true);

function HandleDragStop(){
	if (oDragItem==null) return;

	if (oDragTarget){
		oDragTarget = null;
	} = 1;
	oDragItem = null;

function TouchEnd(e){ = "TouchEnd";

function $(s){
	return document.getElementById(s);

function GetObjPos(obj){
	var x = 0;
	var y = 0;
	var o = obj;
	var w = obj.offsetWidth;
	var h = obj.offsetHeight;
	if (obj.offsetParent) {
		x = obj.offsetLeft
		y = obj.offsetTop
		while (obj = obj.offsetParent){
			x += obj.offsetLeft;
			y += obj.offsetTop;
	return {x:x, y:y, w:w, h:h, o:o};

//Drag and Drop Events
function OnTargetOver(){ = "3px solid red";

function OnTargetOut(){ = "";

function OnTargetDrop(){"";
	if (navigator.platform=="iPad") MakeDragable(oDragItem);


I created two CSS classes to make drag-able elements not selectable and to have the cursor suggest that the element is drag-able.

   -moz-user-select: -moz-none;   
   -khtml-user-select: none;   
   -webkit-user-select: none;   
   -o-user-select: none;   
   user-select: none;
   width: 100px; 
   padding: 3px;

    width: 200px; 
    background-color: LightBlue;
    border: 3px solid white;

I hope someone might find this example useful.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Igor Krupitsky
Web Developer
United States United States
Igor is a business intelligence consultant working in Tampa, Florida. He has a BS in Finance from University of South Carolina and Masters in Information Management System from University of South Florida. He also has following professional certifications: MCSD, MCDBA, MCAD.

You may also be interested in...

Comments and Discussions

QuestionHow to Write Code for MultiSelect from panel to another panel Pin
Anil Honey 20615-Mar-14 7:42
memberAnil Honey 20615-Mar-14 7:42 
QuestionThe code within the article is completely wrong... Pin
Tom Dyer20-May-13 23:58
memberTom Dyer20-May-13 23:58 
QuestionDrag-Drop Implementation on iPad Pin
Rajroshan Sawhney29-Jan-13 20:00
memberRajroshan Sawhney29-Jan-13 20:00 
Hi Igor,
Many thanks for posting this article on Drag-Drop implementation .

I had a question regarding the draggable elements: What I essentially want is for the draggable elements to come back to their original position if they are not dropped in any of the drop-targets. I am looking at the "OnTargetOut()" function as well as the "HandleDragStop()" function.

Any comments/suggestions would be helpful.

Thanks Again! Smile | :)
QuestionDrag Drop Javascript Pin
Daniel H Oostra19-Jun-12 7:02
memberDaniel H Oostra19-Jun-12 7:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170813.1 | Last Updated 21 Apr 2012
Article Copyright 2012 by Igor Krupitsky
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid