wizztjh
wizztjh

Reputation: 7041

Jquery object compare problem

How to compare two Jquery object?

$('<p></p>')[0] === $('<p></p>')[0]
false

$('<p></p>') == $('<p></p>')
false

$('<p></p>').get() == $('<p></p>').get()
false

Upvotes: 1

Views: 681

Answers (4)

andyb
andyb

Reputation: 43823

The following returns true

$('<p></p>').html() == $('<p></p>').html();

Is that what you need?

Edit: The old jQuery group^ discussion on this suggests comparing the child nodes in plain JavaScript since each jQuery object is an array of references to DOM objects. This function was also the accepted answer on this SO question.

^Tried the new jQuery forum but it has not imported the discussion correctly.

Upvotes: 1

mVChr
mVChr

Reputation: 50177

$('<p></p>')[0].outerHTML === $('<p></p>')[0].outerHTML;   // true
$('<p>hi</p>')[0].outerHTML === $('<p></p>')[0].outerHTML; // false

Upvotes: 1

Alex Pacurar
Alex Pacurar

Reputation: 5861

$('<p>') // it creates a new dom element. 
         //Equivalent to document.createElement('p')

so the two

$('<p></p>')[0] and $('<p></p>')[0]

are in fact two distinct DOM elements.

Upvotes: 1

wizztjh
wizztjh

Reputation: 7041

I found a stupid solution ... anyone got better one?

$.md5($('<p></p>').get()[0].toString()) ==$.md5($('<p></p>').get()[0].toString())

Upvotes: 0

Related Questions