Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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')