JavaScript Drag and Drop example 3

School timetable is example of how to use REDIPS.drag library. Page layout contains two tables: left table with school subjects and timetable on the right. After subject is placed to the timetable, button in the same color will be displayed next to the subject (clone object).

REDIPS.drag example03

Please note two checkboxes in the upper left corner of timetable. First checkbox is turned on by default to enable cloning subjects across a week. You can turn it off for placing single subject to the timetable. If second checkbox is checked, then “subject report” will pop up if report button (button next to subjects in left table) is clicked. At the same time, all other subjects will be hidden. Clicking on any element in left or right table will show up all elements.

The following code shows event.dropped() event handler (with logic for cloning DIV elements across a week).

rd.event.dropped = function () {
    var objOld = rd.objOld,                // original object
        targetCell =,         // target cell
        targetRow = targetCell.parentNode, // target row
        i, objNew;                         // local variables
    // if checkbox is checked and original element is of clone type
    // then clone spread subjects to the week
    if (document.getElementById('week').checked === true &&
        objOld.className.indexOf('clone') > -1) {
        // loop through table cells
        for (i = 0; i < targetRow.cells.length; i++) {
            // skip cell if cell has some content
            // (first column is not empty because it contains label)
            if (targetRow.cells[i].childNodes.length > 0) {
            // clone DIV element
            objNew = rd.cloneObject(objOld);
            // append to the table cell
    // print message only if target and source table cell differ
    if ( !== { 
        printMessage('Content has been changed!');
    // show / hide report buttons

Source code (including school timetable with save/recall table using PHP and MySQL) and detailed description of library can be found on Drag and drop table content with JavaScript.

171 thoughts on “JavaScript Drag and Drop example 3

  1. Sorry! I think I meant to say “get the value of attributes”, not “get elements”. :)

  2. Hi,

    Is it possible to save content from more than one table? I see var content = REDIPS.drag.saveContent(‘table2’); in the script.js but how will I be able to differentiate between the two or more table ids?

    Right now the tbl_id in redips_timetable auto increments, could I use that to differentiate for each table?

  3. Never mind, I found how to do it with your AJAX example. Thanks for providing this library to the public!

  4. Hello.
    I try to fill left table (table1) with javascript and it doesn’t work (not draggable)
    var eltab=document.getElementById(‘table1′);
    var rc=eltab.rows.length;
    // delete row except 2 first
    for(var x=rc-1;x>1;x–)
    {eltab.deleteRow(x);} // OK
    var newRow=eltab.insertRow(2);
    var newCell0=newRow.insertCell(0);

    On my screen it’s correct but I cannot drag it.

    Thank you.

  5. newCell0.innerHTML=”something”;

    On my screen it’s correct but I cannot drag it.

    Thank you.

  6. Hello,
    OK I understand why and it runs correctly now.
    I can save the clones by savecontent in Mysql -> it runs.
    But now I want to read Mysql to see the clones I wrote before.
    Is it possible ?

    Thank you.

  7. Hi, How can I extend the code that I get a dynamic calendar for the whole year with all days and weeks? Is it also possible to give to all the table cells in the schedule an automatic unique ID? Thank you so much :)

  8. How do you save two subjects in the same cell if you change drop mode to multiple?

  9. Hi. i am using this code in my administration area. I put multiple teachers in every day and hour (about 5) and it result in more than 100 positions in one week. The question is:
    method GET is not good for me, it truncate at about 100 rows (too limited). I need to use POST method for content variable in script.js and read that variable in db_save.php, but i have no idea, $.ajax give my an error ($ not defined). Can you help with this, please? Thank you!

  10. @cata mitu – Please see method in example22. In this example form elements are send to the server using POST method. This code can be slightly modified for your case because AJAX call functionality is already built-in in REDIPS.drag lib.

    // method parses form elements and submits to the server = function (button) {
        var frm = document.getElementById('myform'),
            params = '',
        // prepare all form elements like name1=value1&name2=value2&name3=value3...
        for (i = 0; i < frm.elements.length; i++) {
            // set element reference
            el = frm.elements[i];
            // if form element is input text
            if (el.type === 'text') {
                params += + '=' + el.value + '&';
        // cut last '&' from params string
        params = params.substring(0, params.length - 1);
        // make ajax call and set redips.handler2() as callback function 
        redips.rd.ajaxCall(redips.ajaxSave, redips.handler2, {method: 'POST', data: params});

    @SIVA SEMMANKADU GANESAN – Client code is the same no mater what is on the server side. Server side should accept parameters, process and save to the database. Please see example03 where is prepared case for the PHP and MySQL.

Leave a Comment