JQuery extension

I am trying to add some existing functions to a site that uses jQuery(I want the function to draggablework with live()).

I found some code that does this, but I am having problems with its operation.

(function ($) {
   $.fn.liveDraggable = function (opts) {
      this.live("mouseover", function() {
         if (!$(this).data("init")) {
            $(this).data("init", true).draggable(opts);
         }
      });
   };
}(jQuery));

I added the code after I downloaded jQueryand jQuery UI, and before I really do anything with it, however, when I try to use it, I get the following error:

$('.myclass').liveDraggable({}) is undefined.

Can anyone help?

+1
source share
2 answers

Your code is working fine. $('.myclass').liveDraggable({})will return undefinedbecause there is no return statement. Usually you want return thisyou to bind calls.

(function ($) { 
   $.fn.liveDraggable = function (opts) {
      return this.live("mouseover", function() {
         if (!$(this).data("init")) {
            $(this).data("init", true).draggable(opts);
         }
      });
   };
}(jQuery));

jQuery.

$('.myclass').liveDraggable({}).live('click', function(){
  // code
});
+4

jQuery UI script (), ( ​​jQuery), . .

, SO-, : jQuery Drag And Drop

+4

All Articles