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

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.