0

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?

shuji
  • 7,369
  • 7
  • 34
  • 49
  • 1
    One jQuery object is not equal to another one, they are two different objects even if they contain the same DOM elements, however comparing plain JS DOM elements will work – adeneo Dec 10 '13 at 21:51
  • But they are supposed to be the same. – shuji Dec 10 '13 at 21:52
  • So every selector return a different object? – shuji Dec 10 '13 at 21:53
  • 2
    Indeed -> http://jsfiddle.net/zV52E/ – adeneo Dec 10 '13 at 21:54
  • 2
    But DOM nodes are not plain objects, so they can be compared -> http://jsfiddle.net/zV52E/1/ – adeneo Dec 10 '13 at 21:55
  • but @adeneo `this` within the handler is a DOM node, not jQuery object. Your first fiddle compares elements wrapped in `$()` – charlietfl Dec 10 '13 at 22:10
  • And so is the OP, at least according to the two lines which looks like they are from a console log. – adeneo Dec 10 '13 at 22:10
  • Yes in my example this is a jQuery object. I'm sorry for the confusion, but it's not actually a mousedown, I'll correct that. – shuji Dec 10 '13 at 22:11
  • @adeneo yup...wasn't paying attention to what `op.selectedNode` was.... would work with `this==opt.selectedNode[0]` assuming only one `circle` in page – charlietfl Dec 10 '13 at 22:12

0 Answers0