views:

54

answers:

2

Using Jquery UI to make a form where when you drag an image to an input, the alt text is parsed as the value of the input.

I have this working fine for 1 image but, I want to make it so it pulls for the current image ( so that I dont have to specify the ID's of all of the images ). See the script below:

<script>    
    $(function() {
    var teamname = $(".helmet").attr("alt");

        $( "#draggable" ).draggable();
        $( "#draggable2" ).draggable();
        $( "#droppable" ).droppable({
            drop: function( event, ui ) {
                $( this )
                    .find( "input" )
                        .val("" + teamname + "");
                }
        });
        $( "#droppable" ).droppable({
            out: function( event, ui ) {
                $( this )
                    .find( "input" )
                        .val( " " );
                }
        });
    });
    </script>

And here is the HTML

<div id="draggable" class="ui-widget-content">
    <img src="team1.jpg" alt="Team Name 1" class="helmet">
</div>

<div id="draggable2" class="ui-widget-content">
    <img src="team2.jpg" alt="Team Name 2" class="helmet">
</div>

<div id="droppable" class="ui-widget-header">
    <input type="text" name="team" />
</div>

Right now, whether I drag the first or second image, its only grabbing the alt text of the first image. Any help would be greatly appreciated.

+1  A: 

Change this function, oh and you can add the out function in with it:

$( "#droppable" ).droppable({
    drop: function( event, ui ) {
        $( this )
            .find( "input" )
                .val("" + ui.helper.find('img').attr('alt') + "");
    },
    out: function( event, ui ) {
        $( this )
            .find( "input" )
                .val( " " );
    }
});

defining teamname outside of the droppable function sets it's definition to the first .helmet it finds.

fudgey
@fudgey - So, I did that and now neither of the images is draggable.
Batfan
Opps, sorry.. I had to change the drop function a bit... here is a "demo" without the images - http://jsfiddle.net/Mottie/EhUNN/2/
fudgey
@fudgey - Awesome, that works :) Just one more quick question, is there a way to make the 'revert' option work in conjunction with the 'out' option? For example, if the user drags an image to the input and then changes their mind, I would like them to be able to drag the input out of the box and have it return to it's default position (while also removing the value that it input)
Batfan
fudgey
@fudgey - Hmmm, neither of those solutions is going to work. It's not the end of the world, though. Thought it would be nice that if the user dragged the item back to the approximate area it was before, the draggable item would revert to it's exact original location when the user "dropped" it.
Batfan
@fudgey - Thanks again for you help :)
Batfan
+1  A: 

Since you have multiple draggable elements then things are easier when using a class instead of an id.

<div class="ui-widget-content draggable">
    <img src="team1.jpg" alt="Team Name 1" class="helmet">
</div>

<div class="ui-widget-content draggable">
    <img src="team2.jpg" alt="Team Name 2" class="helmet">
</div>

<div id="droppable" class="ui-widget-header">
    <input id="team-name" type="text" name="team" />
</div>

<script type="text/javascript">
$(function() {

    $.each($( ".draggable" ), function() { $(this).draggable(); });

    $( "#droppable" ).droppable({

        drop: function( event, ui ) {
            $("#team-name").val($("img", ui.draggable).attr('alt'));
        },

        out: function( event, ui ) {
            $("#team-name").val("");
        }
    });
});
</script>
Saul
@Saul - Your example also made my draggable items un-draggable.
Batfan
@Batfan: Ah yes, the image sources got changed. It's fixed now. But you need to copy the HTML part with div-s also because it's a bit different compared to the original.
Saul