unni
unni

Reputation: 2991

JQuery droppable() does not recognize "this" object reference

I have an Object like this:

function ImageHolder(Attributes, Parent)
{
    this.Id = Attributes['Id'];
    this.Parent = Parent;

    this.Element = $('<div/>');

        this.msg = "hello world";

    this.Parent.append(this.Element);


    this.handleDrop = function(e, ui)
    {
        alert(this.msg);

    };

    this.Element.droppable({drop: this.handleDrop});
}

I then create an object like this:

holder = new ImageHolder(A,B);

But when I try and drop something onto the Element, I get this error:

this.msg is undefined

Am I doing something wrong here?

Upvotes: 1

Views: 124

Answers (1)

PaulMrG
PaulMrG

Reputation: 1822

Make a copy of this;

var thisCopy = this;

before the following function, then replace like so...

this.handleDrop = function(e, ui)
{
    alert(thisCopy.msg);

};

Upvotes: 2

Related Questions