I am in a serious bind. I want to toggle the visibility of some elements I am pulling from an API. The API allows me to make a get request for stories and I am pulling the headlines and images. I can get up to 15 requests at a time from the API. What I want to do is show one request at a time and not all of them at once. When I make the request I get them all at once.
I was thinking of putting the images and headlines into a list and making each child visible one at a time. My jQuery code is below:
var main = function () {
var url = 'http://devapi.xxxxxxx.com/v1/en/stories/latest?format=json&apikey=xxxxxxxxxxxxxxxx';
$.getJSON(url, function(xxxxxx_response) {
//console.log(xxxxx_response);
xxxxxxxxxx_response.stories.forEach(function(data) {
console.log(data);
var $img = $("<img>");
var $p = $("<p>");
$img.attr({
"src": data.largeimage,
"hidden": true,
"id": []
});
$p.attr("hidden", true).append(data.title['#cdata-section']);
$("main .story").append($img, $p);
$("button").click(function(img, p) {
//$img.attr('hidden', false);
//$p.attr('hidden', false);
//s$img.slideToggle('slow');
$img.attr('hidden', false);
$p.attr('hidden', false);
});
});
});
}
$(document).ready(main);
And here is my HTML:
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="">
<title>Media on the Move</title>
<style type="text/css">
img {
width: 360px;
heigh: auto;
}
</style>
</head>
<body>
<main>
<button>Next</button>
<div class="story"></div>
</main>
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="lib/app.js"></script>
</body>
</html>
Please help!!!