javascriptjqueryjquery-ui-droppable

Jquery 'this' keyword


I am working on a drag and drop project, but whenever I drag an item, it only calls the addsgr(); function. Here is my Jquery code:

$(".productItem").draggable({
    helper: 'clone',
    handle: "productItem"
});

$("#basket").droppable({
    accept: ".productItem",
    drop: function(event, ui){
        $("<div></div>")
            .html(ui.draggable.text())
            .appendTo($(this));
            //work on id

            if(this.id=="brnsgr"){
                addbrnsgr();
            }else if(this.id="sgr"){
                addsgr();
            }else if(this.id="flour"){
                addflour();
            }else if(this.id="bkpwdr"){
                addbkpwdr();
            }else if(this.id="butter"){
                addbutter();
            }




    }
});

Solution

  • You are not comparing you are assigning the variable

    try this

    if(event.target.id=="brnsgr"){
        addbrnsgr();
    }else if(event.target.id=="sgr"){
        addsgr();
    }else if(event.target.id=="flour"){
        addflour();
    }else if(event.target.id=="bkpwdr"){
        addbkpwdr();
    }else if(event.target.id=="butter"){
        addbutter();
    }