How can the Flickr API be utilized with JavaScript functions?

In preparation for a project, we are required to utilize the Flickr API in order to display a collection of photos when a specific category is selected. I have successfully set up my categories on the left side of a flexbox container. However, I am struggling with configuring the system to display the Flickr API results tagged with 'tennis' when the user clicks on the tennis category.

Another issue I have encountered with the Flickr API is that the images lack titles underneath them, which is a necessary feature for my project. Additionally, no matter how much I attempt to adjust them, the images continue to appear in a single column. Any assistance or guidance on these matters would be greatly appreciated.

Being relatively new to coding, I would benefit from a clear explanation of how to resolve these challenges.

Below you will find the code snippet:

$("#tennis").click(function() {
  
  //api search for tennis
  let API_KEY = MY API KEY ";
  let tennisStr = "https://api.flickr.com/services/rest/?method=flickr.photos.search&tags=tennis&per_page=15&format=json&nojsoncallback=1&" + API_KEY;
  let photos = [];
  let nrequest;
  let nreceived;

  $(document).ready(function() {
    console.log(tennisStr);
    $.get(tennisStr, function(data) {
      fetchPhoto(data);
    });
  });

  function fetchPhoto(data) {
    nrequest = data.photos.photo.length;
    nreceived = 0;
    for (let i = 0; i < data.photos.photo.length; i++) {
      let photoObj = {
        id: data.photos.photo[i].id,
        title: data.photos.photo[0].title
      }
      photos.push(photoObj);
      getSizes(photoObj);
    }

    function getSizes(photoObj) {
      let getSizesStr = "https://api.flickr.com/services/rest/?method=flickr.photos.getSizes&format=json&nojsoncallback=1&" + API_KEY + "&photo_id=" + photoObj.id;
      $.get(getSizesStr, function(data) {
        nreceived++;
        photoObj.file = data.sizes.size[5].source;
        photoObj.full = data.sizes.size[data.sizes.size.length - 1].source;
        if (nreceived == nrequest) {
          display(photos);
        }
      });
    }

    function display(photos) {
      let htmlStr = "";
      for (let i = 0; i < photos.length; i++) {
        htmlStr += `<figure><a href=${photos[i].file}'><img src = "${photos[i].file}" height = "200" width = "200"></a><figcaption>photos</figcaption></figure>`;
      }
      $("#flickrphoto").html(htmlStr);
    }
  }
});
.flex-container {
  display: flex;
}

.flex-container>div {
  border: 1px solid black;
  flex-direction: column;
}

#navigation {
  flex-grow: 1;
  text-align: left;
}

#flickrphoto {
  display: flex;
  flex-grow: 2;
  text-align: center;
  flex-wrap: wrap;
  justify-content: center;
  flex-basis: auto;
}

#recenthistory {
  flex-grow: 1;
  text-align: left;
}

header {
  text-align: center;
  background-color: black;
  color: white;
  padding: 4rem;
}

.footer {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  background-color: black;
  color: white;
  text-align: center;
}
<!DOCTYPE html>
<html>

<head>
  <meta charset=utf-8>
  <link rel="stylesheet" type="text/css" href="css/style.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
  <script src="./js/main.js"></script>
  <title>Sports Album</title>
  <header>
    <h1>Sports Album</h1>
  </header>
</head>

<body>
  <div class="flex-container">
    <div id="navigation">
      <h2>Categories</h2><br>
      <div id="nav">
        <div id="tennis">
          <p>Tennis</p>
          </br>
        </div>
        <div id="football">
          <p>Football</p>
          </br>
        </div>
        <div id="swimming">
          <p>Swimming</p>
          </br>
        </div>
      </div>
    </div>
    <div id="flickrphoto">
      <h2>Flickr</h2>
    </div>
    <div id="recenthistory">
      <h2>Recent history</h2>
    </div>
  </div>

  <div class="footer">
    <p>Jasmine</p>
  </div>
</body>

</html>

Preview image of the current progress

Thank you for reviewing my work and providing guidance on addressing the issues I have encountered!

Answer №1

I have discovered some issues in your code:

HTML5:

  1. It is recommended to use only one version of jQuery. In this case, make sure to include the following script:
    <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
  2. The <header> tag should be placed inside the <body> tag.
  3. You can utilize a class attribute instead of using an id for each div. Here's an example:

<div id="nav">
  <div id="tennis" class="request">
    <p>Tennis</p>
    <br />
  </div>
  <div id="football" class="request">
    <p>Football</p>
    <br />
  </div>
  <div id="swimming" class="request">
    <p>Swimming</p>
    <br />
  </div>
</div>

CSS:

  1. The images appear in a column despite editing efforts. Remove the line: flex-direction: column;.

To address this, try modifying your CSS as follows:

.flex-container > div {
   border: 1px solid black;
}

JavaScript/jQuery:

  1. Enclose your jQuery code within $(function(){});. For instance:

$(function()
{
   // Your code goes here...
});
  1. The variable API_KEY should be assigned a string value enclosed in double quotes. For example:
    let API_KEY = "39417c145483a7fb3ee91c5fe5bc93fe";
    .
  2. You can simplify making requests to the Flickr API by setting up a single function using
    $(".request").on("click", function() {
    . Consider utilizing .on("click").
  3. In your fetchPhoto function, replace data.photos.photo[0].title with data.photos.photo[i].title.
  4. Then, within your display function, ensure you print the correct titles by incorporating the following:

<figcaption>${photos[i].title}</figcaption>

Refer to this example below:

Consider implementing something like this:

... (the remaining content has been omitted for brevity) ...

Have a fantastic day!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Which specific file is being requested when a forward slash (/) is placed immediately after the website URL?

Is it just me being clueless, or is there something unusual about this URL format? I've come across URLs like http://www.example.com/?p=1 quite often and I'm curious to know what exactly is being accessed by it. I usually use URLs such as http:// ...

Issue with website header disappearing

I'm currently struggling with an issue - I can't seem to pinpoint the problem. The header of a website template is functioning perfectly in 1280*800 resolution, but it's breaking on larger monitors (1900*1440). The header consists of three d ...

Creation of a Joomla module

I'm currently working on a simple module using jQuery, however I am facing issues with disabling MooTools. In my attempt to resolve this, I utilized the following code in the default.php file: $user =& JFactory::getUser(); if ($user->get( ...

How can I leverage Express, AngularJS, and Socket.io to facilitate broadcasting and receiving notifications?

A new notification system is in the works. To illustrate, User 1 is initiating a friend request to User 2. The technologies being utilized include express.js, angularjs, and socket.io. When User1 clicks the button, a request is sent. On User2's end, a ...

What is the process for adding elements to the parent elements that have been selected using getElementsByClassName?

Here is the JSP code snippet I'm working with: <% while(resultSet1.next()){ out.println("<p class='comm'>"); out.println(resultSet1.getString("answer_content")); ...

The image showcases interactive hover effects that resemble a button popping up

Welcome to my HTML code showcase: <div class="container"> <a target="_self" href="flower_gallery.htm"> <img data-src="holder.js/180x180" style="height:20%;width:100%;float:left;" ...

What is the best way to transfer global Meteor variables to templates and effectively utilize them?

I am in the process of developing a compact, single-page application game that emulates the dynamics of the stock market. The price and behavior variables are influenced by various factors; however, at its core, there exists a finite set of universal varia ...

Error: The function `push` cannot be used on the variable `result` (TypeError)

Here is a snippet from my react component const mockFetch = () => Promise.resolve({ json: () => new Promise((resolve) => setTimeout(() => resolve({ student1: { studentName: 'student1' }, student2: { studen ...

The appearance of my website appears differently depending on the resolution

Just recently, I began exploring the world of HTML/CSS/JS and created a handy tool for my personal use. However, I encountered an issue when viewing it on different resolutions which caused some elements to look distorted. The tool I made allows me to inp ...

Elements on the left side are not properly aligned

I've successfully coded the head banner and news containers below the header. However, I'm facing an issue with my left menus: Instead of aligning properly with the header and news containers, they are overlapping them. Here is the HTML Code: ...

Trouble with the query waypoints extension in a simple demonstration

Can anyone help me figure out why the basic example from the waypoints plugin isn't working for me? Here's a link to the jsfiddle I created: http://jsfiddle.net/ZA8bd/2/ CSS .block1 { margin-top:30px; width: 400px; background: red; ...

Could there be an issue with the directory path for the file?

https://i.stack.imgur.com/m8F9y.pngThe background-url() function was utilized to set the background screen to PurpleCloud.png. Despite multiple attempts, the image is still not being displayed. What could be the issue? Even after understanding file path w ...

HTML5 Boilerplate and optimizing the critical rendering path by deferring scripts and styles

Previously, I constructed my website layouts using the HTML5 Boilerplate method: incorporating styles and Modernizr in the head section, jQuery (either from Google CDN or as a hosted file) along with scripts just before the closing body tag. This is an exa ...

Concealing a division element if there is no content inside of it

As a newcomer to jQuery, I am experimenting with setting up a code that hides a div when the 'innerHTML' is null. However, my attempt using the code below is not working. Can anyone point out where I might be going wrong? if (($("#php-errors").h ...

Showcasing Wordpress blog entries on another webpage

Struggling with understanding how to showcase posts from a Wordpress blog on a website? You're not alone. Imagine you have a website www.site.com and a blog structured in Wordpress located at www.site.com/blog. Your task is to feature the recent post ...

angular-chart custom tooltip positioning issue

Hello everyone! I'm having trouble fixing the tooltip position when hovering over a point. I've searched through various posts on StackOverflow and have tried all the examples provided in my implementation: https://github.com/chartjs/Chart.js/tr ...

Choose a specific inner div element within another div using jQuery

Trying to target a specific div within another div in my HTML structure. Here's how it looks: <div id="Stage_game_page1"><div id="cube0">[...]</div><div id="cube1">[...]</div></div> I am attempting to select #cube ...

The text within my div is spilling over the edges despite my attempts to use text align and overflow properties

body { font-family: Arial; font-size: 15px; } .container { position: relative; max-width: 1800px; height: auto; margin: 0 auto; text-align: center; width: 90%; } .container img { vertical-align: center; } .container .content { pos ...

Problem encountered while trying to publish a post using Iron Router

I'm encountering some difficulties when trying to create a route that allows me to respond to comments (.../comments/:_id/reply) and publish the related post. Below is the code snippet: Publications Meteor.publish('commentUser', function(c ...

Tips for concealing a parent element while inside a span with jquery

Beginner asking for help! Take a look at what I've coded: <div class="Links"> <a href="/testthis1.html" data-id="button1"> <img class="icon" src="/test1.png" alt="test1"> <span>Test 1</span> < ...