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 1

whats the point of chaining .keyup() and .focus() when .keyup() gets the job done?

Can someone explain what the point of chaining keyup() and focus() is instead of just using keyup()? The instructor talks about keeping the code DRY so it seems redundant to chain those 2 methods when having only one gets the job done.

function passwordEvent(){
  if ($(this).val().length > 8) {
    $(this).next().hide();
  } else {
    $(this).next().show();
  }
}

$('#password').on("keyup", passwordEvent);

2 Answers

Ryan Field
PLUS
Ryan Field
Courses Plus Student 21,242 Points

You probably want to chain .keyup() and .focus() because you want to check whether the password is at least eight characters long not only when you type a key on the field, but when the field comes into focus (by clicking or tabbing).

Well, I tried it to find out.

If you take out the .focus(), you will get the error message as soon as you finish typing your first character. You could leave it at .keyup, but it's just not as good of an experience to show the error message after you've started typing as opposed to right before you begin typing. Like, you've started typing and then it goes, "...and, oh, by the way, make sure there are at least 8 characters."