Click here to Skip to main content
15,887,399 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello I've created a search engine in which case you can search any game and a gif would appear. I am trying to add a total of 6 gifs per search but I have failed countless times. I hope my code can clarify what i am trying to do in which i have provided my code below. I would greatly appreciate the help. Thank You



<!DOCTYPE html>
<html>
<head>
  <title>Games website</title>
</head>
<body>
  <h1> Game Search </h1>
  <div class="info">
    <p> Search  </p>
      <form class="gif-form">
        <input type="text" id="form-value" class="search-input-rounded">
        <button type="submit" class="search_button"> Search for Games </button>
        <input type="reset" id="reset_button" value="Reset">
      </form>
      <div class="rando_facts animated bounceIn">
        <img id="here_is_gif" src=""/>
      </div>
  </div>

  <!-- Link in jQuery from CDN -->
  <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>

  <!-- My JavaScript -->
  <script type="text/javascript">

    // This waits for the page to load before calling our jQuery
    $( document ).ready(function(){

      // Part 1 - Collect User Input Using jQuery Click Listener note we use the class (.) of search_button
      $('.search_button').on('click', function(){

        // Collect user by grabbing the input form's value via id (#)
        var userInput = $('#form-value').val().trim();
        
        // Change the input to suit the API (ie change spaces to +)
        userInput = userInput.replace(/ /g, "+");

        // Create the Giphy API URL
        var queryURL = "http://api.giphy.com/v1/gifs/search?q=" + userInput + "&limit=6&api_key=dc6zaTOxFJmzC";

        // Part 2 - Use AJAX to call GIPHY API (note that the .done() function waits for the API to respond)
        $.ajax({url: queryURL, method: 'GET'}).done(function(response){

          // This is the API response data. It's a JSON object of 25 gifs
          console.log(response.data);

          // For simplicity, we will take the first gif (ie. at postion 0)
          var giphyURL = response.data[0].images.fixed_height.url;
          console.log(giphyURL)

          // Now you can pass that into your "here_is_gif" <img> tag using its id (#)
          $('#here_is_gif').attr('src', giphyURL);

        });

        // Part 3 - Clear the Gif using the reset_button id (#)
        $('#reset_button').on('click', function(){
          // Grab the img using the id and change the src to empty to remove the image
          $('#here_is_gif').attr("src",'');
        })


        // If using a jQuery click listner on a Submit button, you need to return false to prevent the default page refresh
        return false;
      })
      

    });

  </script>

</body>
</html>


What I have tried:

I have tried switching a couple of codes around but I was not successful because i am such a novice when it comes to JavaScript I was very unsuccessful and i would really appreciate it if someone can help me add a total of 6 gifs to appear as the user types in a name of a game. Thank you
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900