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
Mikey Neilson
Front End Web Development Techdegree Student 12,642 Pointsjavascript function
hey could someone please guide me through these functions line by line so i understand them? Many thanks in advance
function getCurrentImage (currentImage) {
thisImage = currentImage;
var imageLocation = $(currentImage).attr("href");
//Update overlay with the image linked in the link
$image.attr("src", imageLocation);
//Get child's alt attribute and set caption
var captionText = $(currentImage).children("img").attr("alt");
$caption.text(captionText);
}
function getPrevImage() {
var imageParent = $(thisImage).parent().prev();
if(imageParent.length!=0){
thisImage = $(imageParent).children("a");
// imageLocation = $(thisImage).attr("href");
// $image.attr("src", imageLocation);
}
getCurrentImage(thisImage);
}
function getNextImage() {
var imageParent = $(thisImage).parent().next();
if(imageParent.length!=0){
thisImage = $(imageParent).children("a");
// imageLocation = $(thisImage).attr("href");
// $image.attr("src", imageLocation);
}
getCurrentImage(thisImage);
}
1 Answer
Mikey Neilson
Front End Web Development Techdegree Student 12,642 PointsThanks Anthony that was just what i needed great explanation thank you makes more seance to me now..
Anthony Babich
5,505 PointsAnthony Babich
5,505 PointsDo you want to clarify to us what you specifically need help understanding?
Let's take this function for example;
on the first you are declaring a function and you are calling it "getCurrentImage". in reality it could be called anything you want.
within that function declaring, still first line, within the parenthesis you're saying... "I want to take in a parameter that will be declared outside the function for me to use inside the function, and that parameter is called currentImage".
second line, you are preparing "currentImage" for use inside of your function by storing the value of "currentImage" inside of a variable called thisImage.
third line, you're accessing attributes from currentImage to pull the href value (which is essentially the location where the image is location. i assume you're doing this so that you can later declare the source (location) to your function)
4th line - comment
5th line - Here it is, you're declaring the source location of the image using the .attr method.
Now on the 6th and 7th and 8th line, you're simply taking advantage of extra parameters and things you can add to your image... So just like before we're using a method (.attr) to pull the alt tag for the image and store it inside of $caption.text
Hope this helps a little, if it was something else you needed help with follow-up here and I can try to help or someone else will.