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 trialNoah Fields
13,985 PointsJQuery- how to use next() method for challenge
My immediate thought upon the challenge being issued in this video is that I could traverse the DOM less by using the next() function for pet-name, pet-notes, and pet-species. However, I cannot quite figure out how to make such an idea work.
The relevant code is below (I'm not using a snapshot because the snippet is too small for that to be worthwhile):
$('#add-pet').on('click', function() {
var $name = $('#pet-name');
var $species = $name.next();
var $notes = $('#pet-notes');
Presumably I would apply something similar to $notes once I got it working on $species. However, currently this does not work properly, and I've tried a few slightly different approaches as well. What's the proper way to use the next() function?
2 Answers
Steven Parker
231,269 PointsThe full snapshot allowed me to replicate the issue, but it's actually visible in just this part of "post.html":
<div class="six columns">
<label for="pet-name">Pet Name</label>
<input class="u-full-width" placeholder="Scruffy McGuffagin" id="pet-name" type="text">
</div>
You can see here that the input with id "pet-name" is the last element in a div. So it actually has no "next". From the code I was expecting the inputs to be siblings, but they are nested in separate div's.
So one way to traverse from the name input to the select for species would be:
var $species = $name.parent().next().children("select")
Steven Parker
231,269 PointsThis code is incomplete; the part shown here looks good, but I'd need to see the HTML it is selecting from to be sure.
Noah Fields
13,985 PointsHere's a snapshot of the HTML for the page which the above code is associated with.
Steven Parker
231,269 PointsThere is no "app.js' file in the workspace! That would certainly explain it not working.
If you have any problems after adding the file, be sure to make a fresh snapshot.
Noah Fields
13,985 PointsNo, there is not, but that is because I only snapshotted the HTML. The actual working file includes app.js as well.
Steven Parker
231,269 PointsI did not know you could snapshot only part of a workspace! But to do a complete analysis, all the project code would need to be in the snapshot so the issue can be replicated.
Noah Fields
13,985 PointsHere is the snapshot to the full code. Apologies for the inconvenience.
Noah Fields
13,985 PointsNoah Fields
13,985 PointsAh, I see - thank you!