Content of HTML table cells can be dragged to another cell or another table. It isn’t difficult to define onMouseMove handler and change top / left element styles to move the object. In case with tables, you will have to determine somehow target cell. Attaching onMouseOver handler on TD elements will not work, because browser doesn’t fire events to the elements below the dragged object.
Anyway, after taking care of the current scroll position and calculating TD positions, REDIPS.drag should work in recent major browsers like Google Chrome, Firefox, Safari, Internet Explorer, Opera and mobile devices as well. Click on image below, will open live demo where you can drag green, blue or orange bordered DIV elements, change properties (radio button and check-boxes) and click on “Save” button.
Download latest version redips2.tar.gz

REDIPS.drag example01
In this example “Save” button will scan table content, create query string and send to PHP page. Demo shows how to collect content and accept parameters on the server side. More about accepting parameters you can read at Reading multiple parameters in PHP. “Clone” elements (orange in this demo) will be duplicated first because of “redips-clone” keyword contained in class name. If you drop object on cell named “Trash”, object will be deleted from the table (with or without confirmation). Library has built in autoscroll and option to forbid landing to non empty cells or cells named with class “redips-mark”. Table can contain rowspan / colspan TDs and different background color for every cell.
Here are minimal steps to enable content dragging in table:
- put <script type=”text/javascript” src=”redips-drag-min.js”></script> to the head section
- initialize REDIPS.drag library: <body onload=”REDIPS.drag.init()”>
- place table(s) inside <div id=”redips-drag”> to enable content dragging
- place <div class=”redips-drag”>Hello World</div> to the table cell
Other features of REDIPS.drag library:
- methods and data structure are defined in namespace (easier integration with other JS frameworks)
- all JavaScript code is checked with ESLint
- REDIPS.drag documentation generated with JsDoc Toolkit
- drag and drop table rows
- movable DIV element can contain other HTML code (images, forms, tables …)
- forbidding or allowing TDs marked with class name “redips-mark”
- option to define exceptions and allow dropping certain DIV elements to the marked cell
- option to define single content cell on the table declared with “multiple” drop option
- cloning
- for unlimited cloning add “redips-clone” class name to the DIV object
<div class=”redips-drag redips-clone”>Hello World</div> - to limit cloning and transform last object to the ordinary movable object add ‘climit1_X’ class name
<div class=”redips-drag redips-clone climit1_4″>Hello World</div> - to limit cloning and transform last object to immovable object add ‘climit2_X’ class name
<div class=”redips-drag redips-clone climit2_4″>Hello World</div> - where X is integer and defines number of cloned elements (in previous examples, each climit will allow only 4 cloned elements)
- for unlimited cloning add “redips-clone” class name to the DIV object
- unlimited nested tables support
- dropping objects only to empty cells
- switch cell content
- switching cell content continuously
- overwrite TD content with dropped element
- shift table content
- table cell with “redips-trash” class name becomes trashcan
- enabled handlers to place custom code on events: changed, clicked, cloned, clonedDropped, clonedEnd1, clonedEnd2, dblClicked, deleted, dropped, droppedBefore, finish, moved, notCloned, notMoved, shiftOverflow, relocateBefore, relocateAfter, relocateEnd, rowChanged, rowClicked, rowCloned, rowDeleted, rowDropped, rowDroppedBefore, rowDroppedSource, rowMoved, rowNotCloned, rowNotMoved, rowUndeleted, switched and undeleted
- deleting cloned DIV if the cloned DIV is dragged outside of any table
- enabling / disabling dragging
- animation (move element/row to the destination cell/row)
- added support for touch devices (touchstart, touchmove, touchend)
How REDIPS.drag works?
Script will search for DIV elements (with class name “redips-drag”) inside tables closed in <div id=”redips-drag”> and attach onMouseDown event handler. When user clicks with left mouse button on DIV element, onMouseMove and onMouseUp handlers will be attached to the document level.
While dragging DIV element, script changes its “left” and “top” styles. This is function of the onMouseMove handler. When user releases left mouse button, onMouseUp event handler will unlink onMouseMove and onMouseUp event handlers. This way, browser will listen and process mousemove events only when DIV element is dragged.
As I mentioned, onMouseDown is defined on the elements you want to drag. Elements beneath the dragged object will not be able to catch onMouseOver event. Why? Because you are dragging object and that object only can catch the onMouseOver event.
So, to detect destination table cells, script calculates all cell coordinates (with scroll page offset) and store them to the array. Array is searched inside onMouseMove handler and after left mouse button is released, DIV will drop to the current (highlighted) table cell.
In redips2.tar.gz package you will find many examples including example of how to save/recall table using PHP and MySQL. Package also contains and redips-drag-min.js – a compressed version of REDIPS.drag library (compressed with Google Closure Compiler).
Happy dragging and dropping!
@Jerry – REDIPS.drag library works with one DIV element. Only one element can be dragged in time. But every DIV element could have checkbox. Before dragging you could check elements that should be moved. In myhandler_dropped() after one element is dropped to the destination, custom JS code will relocate other checked elements.This is just an idea that seems to be possible ;)
@Mat – Yes it’s possible to change style of dropped element depending on where is dropped. Let’s assume your table cells have assigned id ‘left’ and ‘right’. Bottom code will do the work. Please try.
Hello dbunic!
Great work you have been doing! Thank you!
I wanted to know if there’s a way drag to a div that changes position while scrolling the page (“sticky”).
I have a div on the right with the contents to drag, and a div on the left where i want to drop. This left div slides down as I scroll the page down.
eg:
When the mouse is over the drop cell in the left div, if the div is in the default position (page load), it works fine. But if I scroll the page down the area where the drop is allowed rains at the top of the page and not the current cell position!
Hi Darko,
thanks so much for your fast reply.
Sadly, i wont get it to work. Your code makes perfect sense to me (i would even extend it with an “center” option) – but still the dropped divs dont get any floating attribute.
Here’s an example of how i used it – is there anything basic i missed out?
When i give out the vars “obj” and “tac” i get obj: [object HTMLDivElement] tac: [object HTMLTableCellElement] – that right?
————-code————-
drag_demo
Source
left
right
short
12345
xxx
————–code—————-
Best,
Mat
addition to my post:
the variables are al right of course, i get the right values with obj.id and tac.id
Also, “obj.style.float” set to “right” or “left”
Still, the div doesn’t bother about that…
mat
did it!
It’s just a minor twist – i have to use ‘cssFloat’ ( or ‘styleFloat’ oft IE) instead of ‘float’ to set the floating…now it works perfectly.
Mat
Hi dbunic :)
I encountered a problem while using your – great BTW – drag library. Let’s imagine we have table in drag div and insinde some cells there are dragging div with images in them. Now, when i click outside the image i want whole div to drag, but when i click on the image i want only this image to drag, to another dragging div.
Hi Darko,
i have still one nut to crack:
How do i get elements, that are dropped in a cell with id=’center’ to float while beeing centered all together in the cell?
The div obviously doesn’t bother about the cell’s text-alignment setting, ‘margin: 0px auto’ doesn’t work either.
Do you see a quick solution for that?
Thanks,
Mat
@Paul – Basic dragging element is DIV and dropping topology is TABLE. Unfortunately this library hasn’t option to drop DIV element to another DIV element. Everything contained in DIV will be dragged with DIV element. You can style DIV element to be invisible so it will look like dragging DIV content.
@Mat – I’m glad first issues are solved. If I understood your question, you want DIV elements in the middle cell to be side-by-side and centered? Originally, dropped elements will be centered but one beneath the other. Maybe you will have to play with “display” option. Here is good source of display declaration: quirksmode: The display declaration
@Pedro – Just to be sure. REDIPS.drag needs main DIV container with id=”drag”. Main DIV container can contain tables with content to drag. Page can have separated containers and content from container1 can’t be moved to container2. Are your left and right tables included in the same DIV container? I understood that left table has position: fixed and that means it is taken out from the normal flow on the page. REDIPS.drag can be modified to support fixed position of the container, but I’m not sure about combination of fixed and static position of two tables at the same time. Unless main DIV container is stretched to the whole page – is this your scenario?
Hi Darko, thank you for your quick reply.
Yes my left and right tables are included in the same DIV container id=”drag”.
Left table position is fixed.
How can it be modified to support fixed position of the container, with the combination of fixed and static position of two tables at the same time?
The main DIV container “drag” wans’t stretched to the whole page; I changed it to be right after the tag and made no diference.
I accept any sugestions, i’m running out of ideas :)
thanks
Pedro
@Pedro – I’m now in modifying process of REDIPS.drag library to support position:fixed for table or scrollable table container. It seems to work. I need a day or two to prepare a new example and make testing. Patience please ;)
Hi guys,
Firstable, thanks for this script it really helped me a lot. Now i have one issue to solve. Everything is working great, as i wanted to, but In end when i save data i dont need the positions i just need the values. Lets say I have dragabble divs with values “cucumber,apple,pinnaple” and i have targets “Vegetable, Fruit”. ANd i need to get in the export: “Vegetable-cucumber, Fruit-apple, Fruit-pinnaple”. Please help me out with this one, I am not really JS master…
Thanks
Hi guys,
It looks like my post didnt come trough… So I will write again and quick. Thank you a lot for this script it really helped me a lot. Now I just want to ask you, is it possible to get result not like the posiiton in table, but like ids? If I have divs “apple, grapefruit, tomato” and table rows with first cells “vegetabel, fruit” and I want to get results: “fruit – apple, fruit – grapefruit, vegetable – tomato” etc. And another thing, would it be possible to save this to the db (actually this is not problem for me) but then when I reload the page, and read data from db, and automaticaly place those divs into cells, will that be in result as well? since i didnt move it by hand?
Thanks !
Tom
@Pedro – New REDIPS.drag version is prepared. Please download and try example 10 Sticky tables from the package.
@Tomas – Do you have working example with fruit/vegetable source elements and 2 rows destination table? You only need a custom “save” function to scan destination table and prepare query string. If you can send me zip or show HTML page with your tables – I will try to create “save” function.
Hello dbunic,
I am working on the floor configurator and i want to implement the drag and drop feature on this configurator. if you see on the link given on the website you see what i mean. i want to drag the image that are in the iframe and drag it over the main prarent designer.
Is that possible using your drag and drop library.
Thanks.
Arvind.
@Arvind Jain – Yes it is possible to drag shapes to the main table. As I peeked to the HTML source, my suggestion will be to use block elements (like DIV) for page layout instead of table. You can find a lot of reasons why Web Page Layouts Shouldn’t Use Tables and the last reason is because REDIPS.drag may not work proper. REDIPS.drag scans tables in DIV id=”drag” to calculate “dropping” positions. In case with nested tables, it’s hard to distinguish table as layout from table needed for REDIPS.drag … In redips2.tar.gz package you will find several examples with DIV layout that may be used as a base for building HTML page. Hope this hints will be useful for you. Bye!
Hello guys from redips.net,
i just wanted to tell you that there is something wrong with the latest update. The problem has something to do with the save content function.
I modified the example 03 with a click button that calls the function save() like in example 01 when you click on it. In the save function iam alerting the returned cotent from the table1.
When you drag an drop a element from example 03 of the left table to the school schedule which is the right table and then click on the save button, everythings works fine. But when you move the before dropped element to an another cell and then click on the save button the save_content functions is alerting not just the only element of the right table with the new row and column, it is returning also the elements of table 1.
could you tell me where the problem is. Thanks
Harrison
@Harrison – Yes, latest release has a bug. I build automatic sort of tables array according to clicked DIV element. It should have better performance but I missed to check save_content() function. I will try to fix it as soon as possible – in a day or two. Thank you for submitting problem description. Cheers!
New version 2.7.1 – bug fixing!
@Harrison – Bug in save_content() is fixed. Please try to download latest version and let me know if everything works as expected.
@Tomas – Text selection inside DIV id=”drag” is fixed for IE7 / IE8.
Hi darko,
now everything works fine great thank you very much for the great work.
Harrison
is there a way to have a drag and drop option on rows, from one table to another table with php backend.