I am trying to detect which cell an object is being dropped into.
<table>
<tr>
<td class="weekday">Sun</td>
<td class="weekday">Mon</td>
<td class="weekday">Tue</td>
<td class="weekday">Wed</td>
<td class="weekday">Thu</td>
<td class="weekday">Fri</td>
<td class="weekday">Sat</td>
</tr>
<tr>
<td class="droppable"> </td>
<td class="droppable"> </td>
<td class="droppable"> </td>
<td class="droppable"> </td>
<td class="droppable"> </td>
<td class="droppable"> </td>
<td class="droppable"> </td>
</tr>
</table>
<div class="draggable">Drag Me</div>
On drop, how do I determine which day the div was dropped into?
I couldn't get dropping to work inside of cells - I even tried putting divs inside each of the cells. If you drop the draggable div into the droppable div this code will work:
$(".draggable").draggable();
$(".droppable").droppable({
drop: function(event, ui) {
$(this).html('Dropped!');
}
});
<table>
<tr>
<td class="weekday">Sun</td>
<td class="weekday">Mon</td>
<td class="weekday">Tue</td>
<td class="weekday">Wed</td>
<td class="weekday">Thu</td>
<td class="weekday">Fri</td>
<td class="weekday">Sat</td>
</tr>
<tr>
<td><div class="droppable">empty</div></td>
<td><div class="droppable">empty</div></td>
<td><div class="droppable">empty</div></td>
<td><div class="droppable">empty</div></td>
<td><div class="droppable">empty</div></td>
<td><div class="droppable">empty</div></td>
<td><div class="droppable">empty</div></td>
</tr>
</table>
<div class="droppable">drop in me!</div>
<div class="draggable">Drag Me</div>
It'd be a lot easier if you made the weekday
cells droppable — then you don't have to calculate the index of the current dropped cell and look up the contents of the day-of-the-week cell.
Also, I think you need to give the cells a width and height in the CSS.
This seems to do what you want, courtesy of the jQuery UI docs:
<style type="text/css">
td {
width: 4em;
height: 4em;
margin: 3px;
}
td.weekday {
background: #fcc;
}
td.droppable {
background: #ccf;
}
div.draggable {
background: #cfc;
padding: 1em;
width: 10em;
}
</style>
<table>
<tr>
<td class="weekday">Sun</td>
<td class="weekday">Mon</td>
<td class="weekday">Tue</td>
<td class="weekday">Wed</td>
<td class="weekday">Thu</td>
<td class="weekday">Fri</td>
<td class="weekday">Sat</td>
</tr>
</table>
<div class="draggable">Drag Me</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js"></script>
<script type="text/javascript">
$(".draggable").draggable();
$(".weekday").droppable({
drop: function() {
alert($(this).text());
}
});
</script>