Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript jQuery Basics (2014) Creating a Password Confirmation Form Perform: Part 2

Aaron Selonke
Aaron Selonke
10,323 Points

Unexpected behavior from comparison operator and the JS-Objects

Here is a snippit of the app.js for this form. The form is working as expected, ie, the <span> element form the confirm password input is hiding when the value of the 'confirm password' input and the 'password' input are equal. I'm happy that its working, BUT

I cannot get the console.log to reflect the same. When logging this line to the console: _value1 === _value2 It will always return false. and in the console _value1 return the value in the input but _value2 returns 'undefined'

Why does the program function as expected, but when returning these values to the console it returns 'false' and undefined?

function match()
{

  function match()
{

  var _value1 = $('#password').val();
  var _value2 = $('#confirm').val();

  console.log('This is the password value ' + _value1);

  console.log('This is the confirm value ' + _value2);

  console.log('The two are equal ' + _value1 === _value2);

  if(_value1 === _value2)
  {
   $(this).next().hide(); 
  }
  else
  {
   $(this).next().show(); 
  }


}


}




var $password = $('#password')
var $confirm = $('#confirm_password');

$confirm.focus(match).keyup(validate);

1 Answer

Tom Holland
Tom Holland
24,288 Points

I'm guessing it's because the selector you are assigning to _value2 should be $('#confirm_password') and not $('#confirm')