Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery objects of the same element are not equal?

Tags:

jquery

This must be something I'm overlooking, but please look at the following page and JavaScript and tell me why, for everything that's holy, jQuery won't return true?

HTML: http://alcatel.scottbuckingham.com/reporting/test.html

<p class="test">hello1</p>

JS: http://alcatel.scottbuckingham.com/reporting/_scripts/collapse.js

;(function($, window, document, undefined) {

        var t = $('.test');
        var s = $('.test');

        console.log(t);
        console.log(s);

        if (t === s) {
            console.log('yes');
        }

})(jQuery, window, document);

I've literally spent hours trying to work it out and reduced it to this, almost 1 === 1statement which won't work.

Any help is highly appreciated!

like image 1000
Reinier Kaper Avatar asked May 03 '13 12:05

Reinier Kaper


2 Answers

Try this - Working Demo --> http://jsfiddle.net/mohammadAdil/tHjgN/

 if(t.is(s)) {
    console.log('yes');
 }

http://api.jquery.com/is/

Or with ===

if (t.get(0) === s.get(0)) { //<--Compare DOM elements instead of jquery object's
    console.log('again yes');
}

Demo --> http://jsfiddle.net/mohammadAdil/tHjgN/1/

like image 146
Mohammad Adil Avatar answered Sep 24 '22 05:09

Mohammad Adil


You can use the jQuery is method.

Description: Check the current matched set of elements against a selector, element, or jQuery object and return true if at least one of these elements matches the given arguments.

if (t.is(s)) {
    console.log('yes');
}

Example fiddle: http://jsfiddle.net/IrvinDominin/q86Sh/

like image 41
Irvin Dominin Avatar answered Sep 20 '22 05:09

Irvin Dominin