-1

I am trying to drag images onto the drop zone, then based on there data attribute append a div to the drop zone.

But at the moment its just appending the image and saying undefined is not a function. I then need to be able to drag the new divs around the drop zone

here is the html

<ul class="tab-list">
                        <li>
                            <img src="img/one.png" data-itemstate="rectangle" />
                        </li>
                        <li>
                            <img src="img/one.png" data-itemstate="circle" />
                        </li>
                        <li>
                            <img src="img/one.png" data-itemstate="triangle" />
                        </li>
</ul>

here is the js

    $('.tab-list img').draggable({
            helper: "clone",
        });

        $('#main').droppable({
            drop:function(event, ui) {
                var item = $(ui.draggable);

                console.log(item.data('itemstate'));

                if(item.data('itemstate') == 'triangle'){
                    var add = '<div class="triangle"></div>';
                }
                add.appendTo($(this));
            }
        });
user3613245
  • 79
  • 2
  • 11

2 Answers2

1

Try this

http://jsfiddle.net/harshdand/bqhhpwuq/

$('.tab-list img').draggable({
            helper: "clone",
        });

        $('#main').droppable({
            drop:function(event, ui) {
                var item = $(ui.draggable);

                var add;
                if(item.data('itemstate') == 'rectangle'){
                    add = '<div class="triangle"></div>';
                }
                $(this).append(add);
            }
        });

css:

img{
    width:50px;
    height:50px;
}

div{
    border:1px solid;
    width:20px;
    height:20px;
}

#main{
    width:200px;
    height:200px;
    border:2px solid;
}
Harsh
  • 1,072
  • 1
  • 10
  • 16
0

You have a couple issues. Here is a fiddle with some corrections and an example...

http://jsfiddle.net/woobntbL/1/

Your first problem is here

if(item.data('itemstate') == 'triangle'){
  var add = '<div class="triangle"></div>';
}
add.appendTo($(this));

Add is not defined where you are trying to use it and it also needs to be a jquery object if you are trying to use appendTo.

There are some other issues but my fiddle has corrections you will obv need to change it to suit your needs.

Scott Mitchell
  • 698
  • 4
  • 11