so I'm trying to make a website that allows users to put in latitude and longitude coordinates to find hiking trails within the area. To do this, I'm using an api to retrieve the hiking trails. The problem is that some of the trails lack images. So I used the code below to fill in the otherwise empty spaces where the image would be
$('img').on("error", function () {
$(this).attr('src', `https://source.unsplash.com/collection/${244339}`);
});
This works but it returns the same randomly generated image repeatedly in place of every missing image. So my question is how can I randomize each image so that each object within the api that lacks an image receives a unique image. To further assist, I'll provide my html and javascript code. Thank you!
<body>
<input id="lat" placeholder="Enter the latitude of your desired hiking location">
<input id="long" placeholder="Enter the longitude of your desired hiking location">
<button value="send" id="submit" onclick="callApi()">Search</button>
</div>
function callApi() {
let latitude = document.getElementById("lat").value;
let longitude = document.getElementById("long").value;
console.log("latitude: " + latitude);
console.log("longitude: " + longitude);
const url = "https://www.hikingproject.com/data/get-trails?lat=" + latitude + "&lon=" + longitude + "&maxDistance=10&key=**************";
console.log(url);
$.getJSON(url, function (data) {
console.log(data);
console.log(data.trails[0].location);
$.each(data, function (index, trails) {
console.log(trails[2])
let i;
for (i = 0; i < trails.length; ++i) {
let image = data.trails[i].imgMedium;
let name = data.trails[i].name
let stars = data.trails[i].stars
let starvotes = data.trails[i].starVotes
$('.card-columns').append('<div class="card bg-dark text-white"><img class="card-img" src="' + image + '"/><div class="card-img-overlay"> <p class="name">' + name + '</p><p class="stars">' + stars + " stars" + '</p><p class="starvotes">' + starvotes + " votes" + '</p></div></div>');
};
$('img').on("error", function () {
$(this).attr('src', `https://source.unsplash.com/collection/${244339}`);
});
})
});
}