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

Not Hiding/Responding. Quadruple checked my code... can't figure out where I'm going wrong.

  // Problem: Hints are shown even when form is valid
  // Soultion: Hide and show them at appropriate times
var $password = $("#password");
var $confirmPassword = $("#confirm_password");
  // Hide events
$("form span").hide();

function passwordEvent(){
    // Find out if password is valid
  if($password.val().length > 8) {
      // Hide hint if valid
    $password.next().hide();
  } else {
      // else show hint
    $password.next().show();
  }
}

function confirmPasswordEvent(){
     // Find out if password and confirmation match
  if($password.val() === $confirm_password.val()) {
     // Hide hint if match
    $confirmPassword.next().hide();
  } else{
     // else show hint
    $confirmPassword.next().show();
}

// When event happens on password input
$password.focus(passwordEvent).keyup(passwordEvent).focus(confirmPasswordEvent).keyup(confirmPasswordEvent);

// When event happens on confirmation input
$confirmPassword.focus(confirmPasswordEvent).keyup(confirmPasswordEvent);

1 Answer

Your confirmPasswordEvent function has a missing closing curly brace in the else block. So it should be:

function confirmPasswordEvent(){
     // Find out if password and confirmation match
  if($password.val() === $confirm_password.val()) {
     // Hide hint if match
    $confirmPassword.next().hide();
  } else{
     // else show hint
    $confirmPassword.next().show();
  }
}

These kind of syntax errors are easier to debug if you just copy and paste your code into something like JS Bin or something. It showed me the error and told me the line number.

Chuck Powers
Chuck Powers
13,155 Points

Looks like there's an error in the variable $confirmPassword inside the if statement too.

It's written:

if($password.val() === $confirm_password.val()) {

But, the variable created at the top was $confirmPassword NOT $confirm_Password ( with underscore). OP never came back, so I assume they caught the mistake anyway.