Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I customize the helper using draggable in jQuery-UI

I want to make a custom element with text taken from the element "being dragged" using a helper function. My problem is that ui is undefined, so I don't know how to get a hold of the source of the drag.

$('.draggable').draggable({
    helper: function(event, ui) {
        var foo = $('<span style="white-space:nowrap;">DRAG TEST</span>'); 
        return foo;
    }
});
like image 875
Cros Avatar asked Nov 01 '10 13:11

Cros


People also ask

How does jQuery UI draggable work?

jQueryUI provides draggable() method to make any DOM element draggable. Once the element is draggable, you can move that element by clicking on it with the mouse and dragging it anywhere within the viewport.

What is helper in jQuery?

These functions assist with common idioms encountered when performing Ajax tasks. Also in: Miscellaneous > Collection Manipulation | Forms.

Why is draggable not working?

You have one of these problems: Your jQuery or jQuery UI Javascript path files are wrong. Your jQuery UI does not include draggable. Your jQuery or jQuery UI Javascript files are corrupted.


1 Answers

The helper function you're applying is invoked the following way:

 $(o.helper.apply(this.element[0], [event]))

That means that this refers to the .draggable you want inside that function, for example:

$('.draggable').draggable({
  helper: function(event) {
    return $('<span style="white-space:nowrap;"/>')
            .text($(this).text() + " helper");
  }
});

You can test it out here.

like image 112
Nick Craver Avatar answered Oct 02 '22 12:10

Nick Craver