Search code examples
javascriptphphtmldrag-and-dropdraggable

How to take the id value of dynamic data when using draggable in HTML?


I have two divs parents where I am displaying some dynamic data from the database. The data are being displayed inside children divs, so, when dragging from one div to the other I would like to take the id of that item as well. For now, I just want to be able to print it in Javascript using the alert function.

Javascript functions:

    function allowDrop(ev) {
        ev.preventDefault();
    }

    function drag(ev) {
        ev.dataTransfer.setData("text", ev.target.id);
    }

    function drop(ev) {
        ev.preventDefault();
        var data = ev.dataTransfer.getData("text");
        ev.target.appendChild(document.getElementById(data));
    }

Html divs with PHP data:

 <fieldset id="div1" ondrop="drop(event)" ondragover="allowDrop(event)">
    <legend style="color:cadetblue; border:white; width:auto; font:bold">Vacant parking spots</legend>
    <?php foreach ($sql_get_vacant_spots_results_ as $row) { ?>
        <div id="free_spots_div" draggable="true" ondragstart="drag(event)" id="drag1" width="88" height="31">
            <labled style="color: white; font:bold; font-size:large;"><?php echo ' Spot: ' . $row['spot_no'] . ' Gate: ' . $row['parking_gate_id'] ?></label>
        </div>
    <?php } ?>
</fieldset>

 <fieldset id="div2" ondrop="drop(event)" ondragover="allowDrop(event)">
    <legend style="color:cadetblue; border:white; width:auto; font:bold">Current parking spots of the customer</legend>
    <?php foreach ($arr_users as $user) { ?>
        <div id="customers_spot_div" draggable="true" width="88" height="31">
            <labled style="color: white; font:bold; font-size:large;"><?php echo ' Spot: ' . $user['spot_id'] . ' Gate: ' .  $user['gate_id'] ?></label>
        </div>
    <?php } ?>
</fieldset>

Solution

  • Currently your for-loop will repeat the elements id. id should be unique in the whole document. Also your div current have two attributes id:

    <div id="free_spots_div" draggable="true" ondragstart="drag(event)" id="drag1" width="88" height="31">
    

    getElementById() will just select the first ocurrance of a matching element.

    Here is a small sample with additional console.log().

    function allowDrop(ev) {
        ev.preventDefault();
    }
    
    function drag(ev) {
        ;console.log('drag', ev.target.id);
        ev.dataTransfer.setData("text", ev.target.id);
    }
    
    function drop(ev) {
        ;console.log('drop', ev.dataTransfer.getData("text"));
        ev.preventDefault();
        var data = ev.dataTransfer.getData("text");
        ev.target.appendChild(document.getElementById(data));
    }
    <fieldset id="div1" ondrop="drop(event)" ondragover="allowDrop(event)">
        <legend style="color:cadetblue; border:white; width:auto; font:bold">Vacant parking spots</legend>
        
        <!-- id are to be unique -->
        <div id="free_spots_div1" draggable="true" ondragstart="drag(event)" width="88" height="31">
            <labled style="font:bold; font-size:large;">parking_gate_id1</label>
        </div>
    
        <!-- id are to be unique -->
        <div id="free_spots_div2" draggable="true" ondragstart="drag(event)" width="88" height="31">
            <labled style="font:bold; font-size:large;">parking_gate_id2</label>
        </div>
    
        <!-- id are to be unique -->
        <div id="free_spots_div3" draggable="true" ondragstart="drag(event)" width="88" height="31">
            <labled style="font:bold; font-size:large;">parking_gate_id3</label>
        </div>
    </fieldset>
    
     <fieldset id="div2" ondrop="drop(event)" ondragover="allowDrop(event)">
        <legend style="color:cadetblue; border:white; width:auto; font:bold">Current parking spots of the customer</legend>
        <div id="customers_spot_div" draggable="true" width="88" height="31">
            <labled style="font:bold; font-size:large;">gate_id</label>
        </div>
    </fieldset>