Alan Araya
Alan Araya

Reputation: 721

Jquery comparing elements

I have an array on javascript and i insert the elements on it like this:

 var parentRow = $(button).parent().parent();
 list.push({ parent: parentRow, detailRow: newRow });

On the click of another button i do the following:

      var parentRow = $(button).parent().parent();
      var detailRow = null;

      for (var i in list) {
        if ($(list[i].parent) == $(parentRow)) {
          detailRow = list[i].detailRow;
        }
      }

The point is: The if comparing to two elements should return TRUE, because they are the same DOM element....the same i added before, but it return FALSE.

I would like to know how i compare this two elements to get TRUE there.

Upvotes: 1

Views: 1210

Answers (3)

Bergi
Bergi

Reputation: 664307

They are not the same objects, because they don't refer to the same jQuery instance.

Simple solution: Don't use jQuery and do it with normal DOM methods.

jQuery solution: Use .is()

Upvotes: 1

user736788
user736788

Reputation:

You need to compare the native elements, not the jQuery-wrapped elements. jQuery's DOM methods returns not the elements themselves but a jQuery object.

if (list[i].parent[0] === parentRow[0]) {

Upvotes: 0

David Downes
David Downes

Reputation: 1205

Try:

if (parentRow.has(list[i])) {

Upvotes: 1

Related Questions