Post by ranilwallace on Mar 15, 2016 10:07:59 GMT 10
Hi everyone. I have a json array for movie details. What i want is for each poster object in the array, i want to make a div with the background image as the poster (the url is in the array). But it DOESNT WORK!! If works find if i just make a paragraph with the poster url but when i make the divs they dont show.. Someone correct my code or something if its wrong: <code> var myarray = [{ "title": "Frozen", "year": "2013", "poster": "https://image.tmdb.org/t/p/w150/jIjdFXKUNtdf1bwqMrhearpyjMj.jpg" }, { "title": "Frozen", "year": "2013", "poster": "https://image.tmdb.org/t/p/w150/jIjdFXKUNtdf1bwqMrhearpyjMj.jpg" }]; displaymovies(myarray); function displaymovies(arr) { var imgpath = ""; var i; for(i = 0; i < arr.length; i++) { imgpath += arr .poster;
}
var movitem = document.createElement("div");
movitem.className = "dpad-focusable";
movitem.id = "recbox";
movitem.tabIndex = "0";
movitem.style.backgroundImage = "url(" + imgpath + ")";
var innermov = document.getElementByid("innermov");
innermov.appendChild(movitem);
}
</code>
Ranil, Lauve Tech
|
|
Post by Prezence on Mar 15, 2016 11:34:31 GMT 10
Do you understand line by line what this code is doing? From here, it looks like you are literally making a string with the poster urls and then making one "movitem" element. You need to make the movitems inside the loop so you get each poster url. I haven't tested but try this function displaymovies(arr) { var imgpath = "";
for(i = 0; i < arr.length; i++) { imgpath = arr[i].poster; var movitem = document.createElement("div"); movitem.className = "dpad-focusable"; movitem.id = "recbox"; movitem.tabIndex = "0"; movitem.style.backgroundImage = "url(" + imgpath + ")"; var innermov = document.getElementByid("innermov"); innermov.appendChild(movitem); } }
Check out our website here. Check out AirBrowse here. Follow us on GitHub here.
Last Edit: Mar 15, 2016 11:37:54 GMT 10 by Prezence
|
|
Post by ranilwallace on Mar 15, 2016 11:46:02 GMT 10
hmm i'll try thanks for your response man. i yes i kinda understood what i was doing but am still learning js soo.. ya haha
Ranil, Lauve Tech
|
|
Post by ranilwallace on Mar 15, 2016 11:58:04 GMT 10
Prezence i tried it. it doesnt work. however if i were to create a paraagraph element it shows.
Ranil, Lauve Tech
|
|
Post by ranilwallace on Mar 15, 2016 12:10:25 GMT 10
Nathan Lecompte Wanna help me out?? Also here is the test I did with the paragraph element and it worked but not with the div element i tried to make here
Ranil, Lauve Tech
|
|
Post by altair on Mar 16, 2016 6:09:32 GMT 10
ranilwallace , I found out the bug...... Your loop was not right...... I fixed it up....
function displaymovies(arr) { var imgpath = ""; var i; for(i = 0; i < arr.length; i++) { imgpath = arr[i].poster; var para = document.createElement("p"); var node = document.createTextNode(imgpath); para.appendChild(node); var element = document.getElementById("mov"); element.appendChild(para); }
}
I hope this fixed your issue.. Your pal, altair
|
|
Post by ranilwallace on Mar 16, 2016 6:49:06 GMT 10
Thanks i'll try that and letcha know
Ranil, Lauve Tech
|
|
Post by ranilwallace on Mar 16, 2016 7:35:29 GMT 10
It still doesnt work..
Ranil, Lauve Tech
|
|
Post by altair on Mar 16, 2016 16:54:32 GMT 10
Wait. I will send my files soon... I did not work in JSFiddle....
|
|
Post by altair on Mar 16, 2016 17:13:50 GMT 10
Last Edit: Mar 16, 2016 17:16:35 GMT 10 by altair
|
|