I am having trouble setting a background image to a div using the API-provided link in my CSS. Even when trying to directly access the image in Chrome, nothing seems to happen. Here's the code snippet:
function getData(responseData) {
var poster = responseData.results[0].poster_path;
appendToDom(poster);
}
function appendToDom(poster) {
var aPoster = $('<div>');
var movie_poster = 'url("' + poster + '")';
console.log(movie_poster);
aPoster.css({
'float': 'left',
'margin': 10,
'margin-left': 37,
'margin-top': 20,
'width': 200,
'height': 300,
'font-size': 36,
'color': 'black',
'background-size': '200px 300px',
'background-image': movie_poster //'url("./../images/question.jpg")'
})
$(main).append(aPoster);
}
Upon checking my console, the URL ("/rwn876MeqienhOVSSjtUPnwxn0Z.jpg") is being returned as expected. I'm using The Movie DataBase API for this operation.
Appreciate any help provided in advance!
Good news! I've managed to resolve the issue. Below is the updated JS file with a list of posters linked to specific names.
$('#search').keypress(function(e) {
if (e.which == 13) {
var movie = $('#search-input').val();
$('form#login').submit();
console.log('onclick fun', movie)
makeCall(movie);
return false;
}
});
function makeCall(aMovie) {
console.log('makecall', aMovie);
link = url + aMovie;
console.log(link);
$.ajax({
method: "GET",
url: link,
success: function(data) {
console.log("the data -->", data);
getData(data);
},
})
}
function getData(responseData) {
for (let i = 0; i < responseData.results.length; i++) {
var poster = responseData.results[i].poster_path;
appendToDom(poster);
}
}
function appendToDom(poster) {
var aPoster = $('<div>');
var movie_poster = 'url("https://image.tmdb.org/t/p/w1280' + poster + '")';
console.log(movie_poster);
aPoster.css({
'float': 'left',
'margin': 10,
'margin-left': 37,
'margin-top': 20,
'width': 200,
'height': 300,
'font-size': 36,
'color': 'black',
'background-size': '200px 300px',
})
if (poster === null) {
aPoster.css({
'background-image': 'url("./../images/question.jpg")'
})
} else {
aPoster.css({
'background-image': movie_poster //'url("https://image.tmdb.org/t/p/w1280/rwn876MeqienhOVSSjtUPnwxn0Z.jpg")' //'url("./../images/question.jpg")'
})
}
$(main).append(aPoster);
}