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

Why not give the span a id and address it directly instead of traversing with next?

This question belongs to Perform: Part 1

3 Answers

Sean T. Unwin
Sean T. Unwin
28,690 Points

This is for few reasons.

One, it's to show the power of next().

Another reason is because not every element will be directly identifiable by an id or may not have a unique class or other attribute. Now, of course if you are the app/site developer you could uniquely identify anything how you wish. Although what if, for example, you want to manipulate something from a third-party, like a Twitter plugin or something? You're probably going to use something like next() because you know the element you want to manipulate, but there is not really an otherwise convenient way to grab it.

Edit: As kabir k mentions, closely tied relationships are also a good reason for next(). Examples of this could be, as shown in the lesson, messages for inputs, or other form item companions, or icons/images and text. Generally, in cases where a 1-to-1 relationship between siblings can be made.

kabir k
PLUS
kabir k
Courses Plus Student 18,036 Points

Also, in addition to Sean T. Unwin's explanation, there is a relationship between the password input element (i.e. #password) and the span element. For example, when something happens to #password, you want it to affect span and since they are siblings, and span follows #password, the traversing method, next() is used.

clear! thanks