Question: I have table in which only one td will be empty . Here user can drag and drop the td elements .I am trying to write condition so that empty td accept only its neighbor td elements.

Can you please suggest me the accept condition so that ui.droppable or empty td accept its neighbor td .

Code:

```
$("#dropdiv #c tr td").draggable({
    appendTo: "body",
    helper: 'clone',
    cursor: "move",
    revert: "invalid"
});

$('#c tr td').droppable({
    accept: function(event, ui) {
        return ($(this).html().trim().length == 0)
    },
    drop:function (event, ui ) {
        $(this).append(ui.draggable.text());
        $(ui.draggable).empty();
    }
}); 

```

From above code if the td is empty then only the td accept ui.draggable .

Please find the demo here:<http://jsfiddle.net/ggbhat/6JKWp/2/>

In demo the middle(5) td is empty , i want only 2,4,6,8 are accepted by 5 .

Answer: 
You can use something like,

```
        $('#c tr td').droppable({
           accept: function(ui, item) {
               if($(this).html().trim().length != 0)
                  return false;

               if($(ui).index()==$(this).index())
                 return true;

               var next=$(this).next().get(0);
               var prev=$(this).prev().get(0);
               var me=ui.get(0);

               if(me==next||me==prev)
               return true;

               return false;  
           },
          drop:function (event, ui ) {
                $(this).append(ui.draggable.text());
                $(ui.draggable).empty();   
          }
       });  

```
If you want to selectively accept or reject elements, assign your neighbouring `td` elements some class or id, then do something like this:

```
    $("#c tr td").droppable({
          accept: function(d) { 
              if(d.hasClass("neighbouring_table_element")){ 
                 return true;
              }
          }
   });

```