jQuery Sortable - Limit number of items in list
To have the function fire every time that you try to drag a new item into a specific list (dynamically) you need to use on()
rather than the receive:
method.
http://jqueryui.com/demos/sortable/#event-receive
$(function() {
$( "#day1, #day2" ).sortable({
connectWith: ".connectedSortable"
}).disableSelection();
$( "#day1" ).on( "sortreceive", function(event, ui) {
if($("#day1 li").length > 10){
$(ui.sender).sortable('cancel');
}
});
});
- DEMO: http://so.lucafilosofi.com/jquery-sortable-limit-number-of-items-in-list
$(function() {
$(".sortables").sortable({
connectWith: '.connectedSortable',
//receive: This event is triggered when a
//connected sortable list has received an item from another list.
receive: function(event, ui) {
// so if > 10
if ($(this).children().length > 10) {
//ui.sender: will cancel the change.
//Useful in the 'receive' callback.
$(ui.sender).sortable('cancel');
}
}
}).disableSelection();
});