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

Issue with JavaScript lightbox

I am having an issue with an overlay I am trying to create with JavaScript. I am trying to recreate the image overlay project from the jQuery Basics course, in JavaScript. Here is my code:

// Problem: When a user clicks on an image, it takes them to a dead end
// Solution: Add an overlay to display the images

 // 1. Add an overlay (append)
//var body = document.querySelector("body");
//
///* http://stackoverflow.com/questions/5677799/how-to-append-data-to-div-using-javascript */
// body.innerHTML = body.innerHTML + "<div id='overlay'></div>";

// 2. Disable the default event of clicking through to the default dead end
var anchor = document.querySelectorAll("a");

for (var i = 0; i < anchor.length; i++) {
  console.log(anchor[i]);
   // 3. Get the images
  var numOfImages = anchor[i].children;
  console.log(numOfImages);
  anchor[i].onclick = function() {
    return false;
  }

}

// 4. Add the images to the overlay
var overlay = document.createElement("div");
overlay.innerHTML = numOfImages;
document.body.appendChild(overlay);


 // 5. Grab the images' (not the linked image) src attribute and use that for the overlay images' caption
 // 6. Add caption to overlay images
 // 7. Style overlay
 // 8. Make overlay disappear when overlay is clicked

Thanks, Aaron