Hello everyone,
I feel so bad about it but I gave up on this assignment and looked at the example code.
The reason I gave up is because I got so confused that I could not continue any more yet I knew I was going in the right direction. I knew that I had to and how to create the loop and use the even object but I just got stuck on one part.
I am going to explain my confusion with the “looping through the images and event handler” part only as I have not yet done the darkening effect part.
- I found the “Steps to complete” the most confusing. For example, in the first section of the “Adding an onclick handler to each thumbnail image”, in this part - “This can be done by running the getAttribute() function on the <img> in each case …” - I could not understand why the function would run on the <img> which is as I understand a different element in the html located in the full-img div when the newImage is the one we need with a location in the thumb-bar div. My question is why is the <img> mentioned here at all when all this section wants us to do is create the following code where <img> or full-img div is not even mentioned; newImage.onclick = function(e) {
var imgSrc = e.target.getAttribute(‘src’);
Please correct me if I am wrong about this.
- This part - “Run a function, passing it the returned src value as a parameter. You can call this function whatever you like.” - I would have probably figured out somehow if I had been a bit more patient and not gotten stuck with the section above.
- The same with the third section.
Since I was doing this all on my on my Iphone, I had to store all the images on an external server online which gave me links to them. So I created an array with the links and accessed it with the loop thus assigning them as attributes to newImage. However, it does not seem to work. Can you please explain why;
Here’s my code;
for ( i = 0; i = 4; i++ ) {
var newImage = document.createElement(‘img’);
var images = [“https://s33.postimg.cc/71rt7bggv/pic1.jpg”, “https://s33.postimg.cc/4kg202bzz/pic2.jpg”,
“https://s33.postimg.cc/tqh06w5kf/pic3.jpg”,
“https://s33.postimg.cc/xa2xwpnpr/pic4.jpg”,
“https://s33.postimg.cc/5mq8ima8v/pic5.jpg”];
newImage.setAttribute(‘src’, images[i]);
thumbBar.appendChild(newImage);
newImage.onclick = function(e) {
var imgSrc = e.target.getAttribute(‘src’);
displayImage(imgSrc);
}
}
function displayImage(imgSrc) {
displayedImage.setAttribute(‘src’, imgSrc);
Another question that still boggles me is that it seems to be okay to call a function and define it later. Not only that but also call a function inside a scope closed by braces which is the loop and define it later outside of that scope. To my knowledge, this contradicts some of the lessons on JS here. I know Chris sent a link for an explanation earlier but since the content there is a lil advanced, I could not understand it. I would appreciate a simple explanation.
By the way, could anyone let me know how long it took them to do this whole assignment? A few hours or days? I was so impatient that I looked at the example after having re-read the steps to complete 3 or 4 times and still remaining confused about it. Is it best to just leave it for a while and get back to it with a fresh mind?
Special thanks to Chrismills and everyone trying to help others who have a hard time grasping the assignment.
Thank you!!!