0

I have custom form for make perfect UX, have one field with button plus to add some details information, this field has validation (avoid the same value of the field) that i need to check DOM if is equal or not. Equality I need is the same DOM, not like code.

HTML:

<form id="form">
    <select> ... </select><span class="btn"> + </span>
</form>

Javascript:

$("#form select").on('change', changeForm );

function changeForm() {
    var check = $(this);
    $("#form select").each(function(){ // In first index is exactly equal of variable check
        if (check == $(this)) { ... } // false
        if (check === $(this)) { ... } // false
        if (check.isEqualNode($(this)) { ... } // Uncaught TypeError: undefined is not a function
        if (check.isSameNode($(this)) { ... } // Uncaught TypeError: undefined is not a function
    }
}

Has code to return true? Or my code doesn't is good way?

Adriano Resende
  • 2,549
  • 1
  • 30
  • 34

2 Answers2

1

You could use .is()

if (check.is(this)) { ... } 
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
0

jQuery constructor returns a new jQuery object and 2 different objects are not considered equal.

You could get the wrapped DOM element and compare it:

if ( check.get(0) === this )
Ram
  • 143,282
  • 16
  • 168
  • 197