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 trialjohn larson
16,594 Pointsslightly off topic, return to the start of a slide show with jQuerry
I'm just trying things out and experimenting. I have a very simple slideshow. Just four img tags. It works but I cant figure out how to get it back to the beginning after the last img. I figure some kind of conditional statement or I saw someone use a .end(), but I can't apply that either. I've scoured the web and all the usual resources. I want to keep it simple as possible.
$(function(){
$("img").click(function(){
$(this).hide().next().show()
})
})
2 Answers
Mike Constantino
12,034 PointsI wouldn't necessarily use .next(), but since this is so simple I don't see a huge problem with it. Here's what we can do with the code you've provided:
$(function(){
$("img").click(function(){
if ($(this).next().length) {
$(this).hide().next().show();
} else {
$(this).hide();
$('#first').show();
};
});
});
Just give the first img an id or class of first. Wrap it in an if-else statement to use JavaScript's native .length to check if it there is a sibling next or not. If there isn't, make it so it loops back to the first. Just make sure all the img tags are in some sort of container (I assume this is already true).
This code isn't perfect. But it'll work! :)
Let me know if you have any questions! :)
john larson
16,594 Pointsso then ($(this).next().length) is shorthand for ($(this).next() < $(this).next().length ) or something like that?
Mike Constantino
12,034 PointsNot exactly, it's basically short for ($(this).next().length === true). Because remember, any number over 0 is true in JS.
john larson
16,594 Points0k, I get it. Thanks again.
Mike Constantino
12,034 PointsYou're very welcome! Let me know if there's anything else. :)
john larson
16,594 Pointsjohn larson
16,594 PointsThanks so much mike! I mostly get what you did, except the .length in this line: if ($(this).next().length) . How does the .length work in this scenario?
Mike Constantino
12,034 PointsMike Constantino
12,034 Points.length is a native JS property. Everything is JavaScript is related to numbers, right? 0 is equal to false, and everything else is usually true. We're using .length here to check the length of the NEXT sibling. If that length is 1 or greater, it means it exists(true), and we can run our code. Otherwise, if the length is 0, it means the sibling is nonexistent and we loop back around to the first