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

Rodrigo Angulo
Rodrigo Angulo
13,378 Points

Can't get appropriate response from confirm password.

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

//Hide hints
$("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() === $confirmPassword.val()){
    //hide hint if matched
    $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);
Rodrigo Angulo
Rodrigo Angulo
13,378 Points

In the video, Mark shows the pop-up "confirm password" when the two values are not matching. When I run this code it does not work. What am I doing wrong?

2 Answers

Steven Parker
Steven Parker
229,005 Points

Without seeing the HTML this is a bit of a guess, but I suspect that on the 2nd line there's a missing pound symbol:

var $confirmPassword = $("confirm_password");  // should this be "#confirm_password" instead?