I'm trying to loop a script that targets a different element each time it loops, while also changing the script slightly each time. I want the script to loop inside #row1 the first time, then #row2 the second time all the way to 10. And I need the script itself to change slightly each time by adding +1 to the "data.children.slice" section. Here is my script:
<script>
$.getJSON(
"https://www.reddit.com/r/images.json?jsonp=?",
function foo(data)
{
$.each(
data.data.children.slice(0,1),
function (i, post) {
$(".redditTitle").append( '<a href="' + post.data.url + '">' + post.data.title + '</a>' );
$(".redditPermalink").append( '<a href="http://www.reddit.com/' + post.data.permalink + '">forum</a>' );
$(".redditUps").append( post.data.ups );
$(".redditDowns").append( post.data.downs );
$(".redditThumbnail").append( '<a href="' + post.data.url + '">' + '<img src="' + post.data.thumbnail + '" alt="Image" class="floatleft image news show">' + '</a>' );
$(".redditUsername").append( '<p class="mediumtext p-color alignleft floatleft redditUsername">'
+ '<a href="https://www.reddit.com/user/' + post.data.author + '">' + post.data.author + '</a>' + '</p>' );
}
)
}
)
.error(function() { alert("error"); })
data.data.children.slice(0,1),
children.slice(0,1)
data.data.children.slice(1,1),
(10,1)
$(document).ready(function(){
var i=1;
var n=0;
$("'#row' + i++")function(){
for {
$.getJSON(
"https://www.reddit.com/r/images.json?jsonp=?",
function foo(data)
{
$.each(
data.data.children.slice(n++,1),
function (i, post) {
$(".redditTitle").append( '<a href="' + post.data.url + '">' + post.data.title + '</a>' );
$(".redditPermalink").append( '<a href="http://www.reddit.com/' + post.data.permalink + '">forum</a>' );
$(".redditUps").append( post.data.ups );
$(".redditDowns").append( post.data.downs );
$(".redditThumbnail").append( '<a href="' + post.data.url + '">' + '<img src="' + post.data.thumbnail + '" alt="Image" class="floatleft image news show">' + '</a>' );
$(".redditUsername").append( '<p class="mediumtext p-color alignleft floatleft redditUsername">'
+ '<a href="https://www.reddit.com/user/' + post.data.author + '">' + post.data.author + '</a>' + '</p>' );
}
)
}
)
.error(function() { alert("error"); })
}
}
});
The id's are not changing because "'#row' + i++"
is a string. This is because it is surrounded by double quotes ("
). If you want it to be treated as an expression, you should write $('#row' + i++)
.
If I understand your question, there may be an easier way to do what you want. Try iterating over the first ten children, and putting the data into the corresponding div at each step. E.g.:
$.getJSON(
"https://www.reddit.com/r/images.json?jsonp=?",
function foo(data) {
$.each(
// iterate over 10 children, starting at the 0th index
data.data.children.slice(0,10),
function (i, post) {
// put data into the corresponding DIV
$('#row' + i).append('<p>' + post.data.title +'</p>');
}
)
}
).error(function() { alert("error"); })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="row0"></div>
<div id="row1"></div>
<div id="row2"></div>
<div id="row3"></div>
<div id="row4"></div>
<div id="row5"></div>
<div id="row6"></div>
<div id="row7"></div>
<div id="row8"></div>
<div id="row9"></div>
Note: you can change the css selector as needed to target the correct HTML element. For example $('#row' + i + ' .redditTitle').append( ... );
would target children of the ith row with class redditTitle
.