0

I have two tables in a below Div. Out of that, I would like to prevent drag and drop functionality on one table and other should have drag and drop functionality.

I was able to successfully stop drag and drop on Div level (please see working code, I found this code from Disable Drag and Drop on HTML elements?) However when I tried to assign similar code to specific table, it does not work and I am able to perform drag and drop on those tables.

HTML:

<div class="test">
    <table id="s_1" summary="Table 1" > </table> 
    <table id="s_2" summary="Table 2" > </table> 
</div>

Java Script:

Working:

/* drag and drop    */

            $('.test').bind("dragover", function(e) {
                e.preventDefault();
                return false;
            });

            $('.test').bind("drop", function(e){
                e.preventDefault();
                return false;
            });

Not Working:

$('.test table').each(function(){ 

    console.log($(this).attr("summary"));  
    if ($(this).attr("summary") == "Table 2") {

        $(this).bind("dragover", function(e) {
                console.log($(this).attr("summary"));  
                e.preventDefault();
                return false;
        });

        $(this).bind("drop", function(e){
                console.log($(this).attr("summary"));  
                e.preventDefault();
                return false;
        });
    }
});
Community
  • 1
  • 1
user1546784
  • 125
  • 1
  • 2
  • 10

2 Answers2

0

You have to cancel the mousedown event:

$(this).on('mousedown', function(e) {
  e.preventDefault();
});

But you can do it inside HTML if you want:

<table id="s_2" summary="Table 2" onmousedown="return false"> </table> 
Starmetal
  • 740
  • 6
  • 14
0

Try this code. This way you get to listen to that event too.

    $(this).on('mousedown', function(e) {
  event.stopPropagation();
});

More info on stopPropogation here

HereToLearn_
  • 1,150
  • 4
  • 26
  • 47
  • Thank you guys.. but with that also it is preventing drag and drop from entire DIV and not specific to have table. – user1546784 Nov 16 '15 at 21:53