So here's the full function/for-loop. I'm basically trying to grab images from Flickr and present them using Lightbox.
If you don't need all this info, just scroll down to "WHAT'S WRONG WITH THIS SYNTAX?" The jQuery .html() method that follows is giving me trouble. How does the syntax work in this instance? Thank you!
$.getJSON(requestURL, function(flickrResponse) {
flickrResponse.items.forEach(function (item) {
// create a new a element and hide it
var $anchor = $("<a>").hide();
// set the href attribute of the a element to connect to the same Flickr image
// that the img element connects to
$anchor.attr("href", item.media.m);
// set the data-lightbox attribute, and use "Flickr" as the value of the attribute
$anchor.attr("data-lightbox", "Flickr");
// create a new JQuery element to hold the image
// but hide it so we can fade it in
var $img = $("<img>").hide();
// set the attribute to the url
// contained in the response
$img.attr("src", item.media.m);
// WHAT'S WRONG WITH THIS SYNTAX?
// Use the jQuery.html() method to set the img element as the innerHTML of the a element.
$($anchor).html($img);
// attach the img tag to the main
// photos element and then fade it in
$("main.photos").append($anchor);
$anchor.fadeIn();
});
});
The corresponding HTML file:
<!doctype html>
<html>
<head>
<title>Flickr App</title>
<link href="stylesheets/styles.css" rel="stylesheet">
<link href="stylesheets/lightbox.min.css" rel="stylesheet">
</head>
<body>
<header>
</header>
<main>
<div class="photos">
</div>
</main>
<footer>
</footer>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="javascripts/lightbox.min.js"></script>
<script src="javascripts/app-lightBox.js"></script>
</body>
</html>
$anchoris already a jQuery object, it's not necessary to use the$()syntax around it. But does it fail if you do? What's the problem you are experiencing?