I have used this example for a Jquery Live Dragable:
jQuery Drag And Drop Using Live Events
But want to convert it to use the .on in jquery 1.7 tried all sorts of permutations can anyone help?
Here is my working code using .live to create a .livedragable method:
(function ($) {
$.fn.liveDraggable = function (opts) {
$(this).live("mousemove.liveDraggable", function() {
$this = $(this);
$this.draggable(opts);
$this.off('mousemove.liveDraggable'); // kill the mousemove
});
return $();
};
}(jQuery));
Here is the call to the livedraggable:
$(".item").liveDraggable({
revert: true
});
I am using this because I have a number of draggables that are loaded via ajax from a DB.
I have now tried:
this.on("mousemove",".liveDraggable", function() {
but it doesn't work.
UPDATE Finally got to this via Joel Purra's answer (see below) and it works great!!
(function ($) {
$.fn.liveDraggable = function (draggableItemSelector, opts) {
// Make the function chainable per good jQuery plugin design
return this.each(function(){
// Start the event listener for any contained draggableItemSelector items
$(this).on("mouseenter", draggableItemSelector, function() {
var $this = $(this);
// If the item is already draggable, don't continue
if($this.data("is-already-draggable") === true)
{
return;
}
// Make the item draggable
$this.draggable(opts);
// Save the fact that the item is now draggable
$this.data("is-already-draggable", true);
});
});
};
}(jQuery));
Plus the selector
$(function() {
$("#my_div").liveDraggable(
".item",
{
revert: true
});
});