Elements passed from Javascript render from bottom to top

Here’s my Pen so far: https://codepen.io/GiaFil/pen/JvGpqN
When I try to render the html variable, the elements render from bottom to top despite appearing from top to bottom the console.log(html) statement. I’m confused as to why this happens because it doesn’t happen here: https://codepen.io/GiaFil/pen/GxaVNb, despite them being similar. Any suggestions?

It’s because ajax requests are asynchronous, so you won’t have the same order as what’s an the array. You could rearrange the elements after the for loop has completed.

That’s not what I am asking. Click the link and you’ll see what I mean.

Im on a phone so codepen is not really usable and I can’t check this, but afaics looks like you’re rerendering to the DOM on each loop iteration: move the .html(html) out of the loop, the loop should just build the string. Also log the html string on each iteration to see how it builds up, that seems to be where the issue lies

Edit: just looking more closely and the whole thing is inside a callback, I see why you are rerendering now, use append not html, you shouldn’t be replacing the html every time.

Using .append made things worse. Now elements repeat, whereas when I used .html it just rendered all elements once, only in the wrong direction for some reason…

Append is logically the correct thing to use here: select an element, then append html to it on each iteration. For it to be repeating suggests something is in the wrong place. Try setting up the html to append inside the loop (the var html = "") so that it starts from scratch each time, then append at the end of each iteration

I’m seriously lost… I put the definition of html inside the loop, but still the same result. The html in the console looks goods though…

Ahahaa, in front of a computer now:

Setup string here:

$.getJSON("https://wind-bow.glitch.me/twitch-api/streams/" + channels[i] +"?callback=?",channels,function(data) {
  var html = ""; 

It is to do with the rendering, use prepend not append

$(".ch_st").prepend(html);

I’m a bit tired and my brain isn’t working properly, I can’t figure out append is going backwards :\

It’s still wrong. The elements are rendered from top to bottom namewise but still upwards. I thought maybe I could render outside the function, if I had access to html outside of it. I tried to return it but it didn’t work.

That renders them in order they are in the array, I’m not sure how else you want them rendered?

Below the channel/status bar.

This took far too long. Why, in the html, is there a negative margin applied to every one of those channels? You get that, every time you render one out, there will be a negative margin, ie it will literally do the exact opposite to a margin with a positive value.

1 Like

FINALLY! It worked. I put negative there,the same as the channel/status bar, because I wanted to be the same stylewise. Thank you for your time and patience!

1 Like

:joy: god that took ages for me to notice. You should be able to just use append, and it should work fine

1 Like