jeffrey desjardins
jeffrey desjardins

Reputation: 25

how to get jquery compare operator working

  //check if the password mataches the password confirmation
    var passMatch = $('input[name="password"]');
    var passConfirm = $('input[name="confirm_password"]');
        passConfirm.on('blur',function(e) {
            console.log(passMatch.val());
            console.log(passConfirm.val());
            if(passMatch == passConfirm) {
                passConfirm.css({backgroundColor: 'lightgreen'});
                
            }else {
                passConfirm.addClass('error_message');
            }       
        })

I could be missing something here - (ok, I'm prob missing something there) but on blur BOTH these fields console.log the same value yet it applies the error_message class as if they didn't? I've tried !=, ==, ===, !== they all resort to the same - why?

Upvotes: 0

Views: 35

Answers (1)

Mike B
Mike B

Reputation: 2776

Even if the elements have the same values, their == comparison will be false as they are different DOM elements. To compare values use the same val() functions that you used in the printouts.

var passMatch = $('input[name="password"]');
var passConfirm = $('input[name="confirm_password"]');
passConfirm.on('blur',function(e) {
    if(passMatch.val() == passConfirm.val()) {
        passConfirm.css({backgroundColor: 'lightgreen'});
        passConfirm.removeClass('error_message');
    } else {
        passConfirm.addClass('error_message');
    }
})

Upvotes: 1

Related Questions