Why is my jquery object changing context?

204 views Asked by At

I am comparing on mousedown event two elements but they are false because of the context property:

console.log(this, opt.selectedNode);
[circle, prevObject: x.fn.x.init[1], context: undefined, jquery: "2.0.3", constructor: function, init: function…]
[circle, prevObject: x.fn.x.init[3], context: document, jquery: "2.0.3", constructor: function, init: function…]

This is what I do on mousedown:

$("circle").ondrag({
    start:function(){
        //this is a jQuery object
        console.log(this==opt.selectedNode); //true the first time, false the second time it's executed
        console.log(this[0]==opt.selectedNode[0]); //always true
        if (this==opt.selectedNode) { //select other node
            $(".selectedNode").removeNSClass("selectedNode");
            if (!point.isFirst()) {
                opt.selectedNode = $("circle").bydata('point',pointarray[pointarray.indexOf(point)-1]).addNSClass("selectedNode"); //if there is a node before
            } else if (!point.isLast()) {
                opt.selectedNode = $("circle").bydata('point',pointarray[pointarray.indexOf(point)+1]).addNSClass("selectedNode"); //if there is a node after
            } else opt.selectedNode = null; //last point, select nothing
        }
    }
});

Why the context changed?

0

There are 0 answers