Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

134
Views
Trying to get one picture at time

I have created a function to create a bear picture but I only want one image at a time. There is a button that is resetting the other API I have but not this one. Any help or suggestions?

const numImagesAvailable = 145 //how many photos are total in the collection
const numItemsToGenerate = 1; //how many photos you want to display
const imageWidth = 360; //image width in pixels
const imageHeight = 360; //image height in pixels
const collectionID = 9396519 //Bears, the collection ID from the original url
const galleryContainer = document.querySelector('#gallery-item')

function renderGalleryItem(randomNumber) {
  fetch(`https://source.unsplash.com/collection/${collectionID}/${imageWidth}x${imageHeight}/?sig=${randomNumber}`).then(function(response) {
    console.log(response)
    let galleryItem = document.createElement('img');
    galleryItem.className = "center-bear";
    galleryItem.setAttribute("src", `${response.url}`)
    document.body.append(galleryItem)
  })
}

for (let i = 0; i < numItemsToGenerate; i++) {

}

buttonEl.addEventListener("click", function(event) {
  event.preventDefault();
  listItemEl.remove();
  callapi();
  let randomImageIndex = Math.floor(Math.random() * numImagesAvailable);
  renderGalleryItem(randomImageIndex);
});
<div id="gallery-item"></div>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

const numImagesAvailable = 145; //how many photos are total in the collection
const numItemsToGenerate = 2; //how many photos you want to display
const imageWidth = 360; //image width in pixels
const imageHeight = 360; //image height in pixels
const collectionID = 9396519; //Bears, the collection ID from the original url
const urlPrefix = `https://source.unsplash.com/collection/${collectionID}/${imageWidth}x${imageHeight}/`;

const galleryContainer = document.querySelector("#gallery-item");
const button = document.querySelector("#img-getter");

function renderGalleryItem(index) {
  const url = `${urlPrefix}?${index}`;
  
  fetch(url).then(function (response) {
    const img = document.createElement("img");
    img.className = "center-bear";
    img.setAttribute("src", `${response.url}`);
    galleryContainer.append(img);
  });
}

button.addEventListener("click", function (event) {
  // remove old images
  galleryContainer.replaceChildren();
  
  // get new images
  for (let i = 0; i < numItemsToGenerate; i++) {
    const index = Math.floor(Math.random() * numImagesAvailable);
    renderGalleryItem(index);
  }
});
<button id="img-getter">Click</button>
<div id="gallery-item"></div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!