views:

32

answers:

3

if i have:

<script type="text/javascript" charset="utf-8">
    function test(){alert('test');}
</script>

<div id="drag_object">

</div>

<div id="drag_over">

</div>

how can i trigger test() when drag_object is dragged over drag_over using jQuery?

thanks :)

A: 

See the jQueryUI site regarding visual feedback on droppables.

Jason
A: 

Check out jQuery UI's "draggable" and "droppable": http://jqueryui.com/demos/draggable/ http://jqueryui.com/demos/droppable/

Alison
i need to manipulate the DOM by adding a div with dynamic text in it using test() and i can't see how to implement this using jqueryUI?
significance
if you are using jQuery UI's draggable droppable then there is a event called 'drop' which triggers after #drag_object is dropped into #drag_over. This event can easily call your function.Your code might look like this: $("#drag_object").draggable(); $("#droppable").droppable({ drop: test(); });source:http://jqueryui.com/demos/droppable/#event-drop
lnrbob
ah yes - except using 'over'! thanks.if you want to put that in a answer i will mark it as correct.thanks :)
significance
that's what I wrote in my answer
Rodrigo Alves
Glad that helped, just expanding on Alison and Jason's answers ;)
lnrbob
@rodrigo and in mine. it's pretty easy to look at the demo they have there and apply it to whatever you require.
Jason
A: 

You can do this using the jQuery UI's Droppable and Draggable

What you want is to add the Droppable's Over event which can be done either this way:

$( ".selector" ).droppable({
    over: function(event, ui) { ... }
});

or this way:

$( ".selector" ).bind( "dropover", function(event, ui) {
 ...
});

I prefer using the first method but it's except to you. So, for your example

<meta charset="UTF-8" />    
    <style type="text/css">
    #drag_object{ width: 100px; height: 100px; padding: 0.5em; float: left; margin: 10px 10px 10px 0; }
    #drag_over{ width: 150px; height: 150px; padding: 0.5em; float: left; margin: 10px; }
    </style>
    <script type="text/javascript">
    $(function() {
        $("#drag_object").draggable();
        $("#drag_over").droppable({
            over: function(event, ui) { alert('You dragged me over'); }
        });

    });
    </script>


<div class="demo">

<div id="drag_object" class="ui-widget-content">
    <p>Drag me to my target</p>
</div>

<div id="drag_over" class="ui-widget-header">
    <p>Drop here</p>
</div>

</div><!-- End demo -->

You can check this out: http://jsfiddle.net/TYrT8/

Rodrigo Alves