Storing a max of 6 cards per page

I am making a pokedex API. I am now able to load all the cards by name or type. But the code loads in all the cards at one page. I would like to store the cards with a max of 6 and then they continue on a new page. But I can’t find how to do this so if anyone has an idea let me know!

JS:

$(document).ready(function (e) {

$("#TheSubmitButton").on("click", function(event){

  $("#card-container").empty();
  event.preventDefault();
  var pokemon = $("#searchInput").val().trim();

  $.ajax({
    method: "GET",
    url: "https://api.pokemontcg.io/v2/cards?q=name:" + pokemon

  }).then(function(response){
    for (var i = 0; i < response.data.length; i++) {

      var pokeTainer = $("<div class='poketainer'></div>");

      var pokemonName = $("<div></div>");
      pokemonName.text(response.data[i].name);
      pokeTainer.append(pokemonName);

      var pokemonCard = $("<img class='pkmn-card'>");
      pokemonCard.attr("src", response.data[i].images.small);
      pokeTainer.append(pokemonCard);
                  
      var pokemonEvolve = $("<div></div>");
      pokemonEvolve.text('Evolves to: ' + response.data[i].evolvesTo);
      pokeTainer.append(pokemonEvolve);
                  
      var pokemonFlavor = $("<div></div>");
      pokemonFlavor.text(response.data[i].flavorText);
      pokeTainer.append(pokemonFlavor);

      var pokemonTypes = $("<div></div>");
      pokemonTypes.text(response.data[i].types);
      pokeTainer.append(pokemonTypes);
                  
      $("#card-container").append(pokeTainer);
        
    }
  });

});

Answer

Here’s a very basic snippet (including pagination):

const page = document.getElementById('card-container')

// the funcftion that fetches data from the API
const fetchData = ({
  name,
  pageNumber
}) => {
  return fetch(`https://api.pokemontcg.io/v2/cards?q=name:${name}&page=${pageNumber}&pageSize=6`)
}

// constructing the HTML, based on the
// JSON returned from the API
const pageHtml = ({
  data
}) => {
  let html = ''
  data.forEach(pokemon => {
    html += '<div>'
    html += `<div>${pokemon.name}</div>`
    html += `<div><img src="${pokemon.images.small}" /></div>`
    if (pokemon.evolvesTo) {
      html += `<div>Evolves to: ${pokemon.evolvesTo}</div>`
    }
    if (pokemon.evolvesFrom) {
      html += `<div>Evolves from: ${pokemon.evolvesFrom}</div>`
    }
    html += `<div>${pokemon.flavorText}</div>`
    html += `<div>${pokemon.types}</div>`
    html += '</div><hr />'
  })
  return html
}

// the update function:
// fetching & then updating the view
const updatePage = (searchObj) => {
  fetchData({ ...searchObj
    })
    .then(resp => resp.json())
    .then(json => {
      page.innerHTML = pageHtml(json)
    })
}

// basic search parameters
let searchObj = {
  name: '',
  pageNumber: 1,
}

jQuery(document).ready(function($) {

  // updating the name search parameter
  $('#searchInput').on('input', function(e) {
    searchObj = {
      ...searchObj,
      name: $(this).val().trim()
    }
  })

  // submitting the query, based on the current
  // search parameters
  $('#TheSubmitButton').on('click', function(e) {
    updatePage(searchObj)
  })

  // previous page
  // NOT PROTECTED FROM overflow
  $('#prevPage').on('click', function() {
    searchObj = {
      ...searchObj,
      pageNumber: searchObj.pageNumber - 1
    }

    updatePage(searchObj)
  })
 
  // next page
  // NOT PROTECTED FROM overflow
  $('#nextPage').on('click', function() {
    searchObj = {
      ...searchObj,
      pageNumber: searchObj.pageNumber + 1
    }

    updatePage(searchObj)
  })
  
})
.pagination {
  display: flex;
  width: 30%;
  justify-content: space-between;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div>
  <input id="searchInput" type="text" placeholder="POKEMON NAME" /><br />
  <button id="TheSubmitButton">SEARCH</button>
</div>
<div class="pagination">
  <button id="prevPage">PREV</button>
  <button id="nextPage">NEXT</button>
</div>
<hr />
<div id="card-container"></div>
  • the search parameters are kept in a parameter object (so you can easily extend it to maxNumPages, hasPrev, hasNext, etc.)
  • the fetch function is extracted, so you just have to pass in parameters & it fetches the data
  • the HTML is created in a separate function (like a template, that you can easily modify)
  • the pagination (prev page, next page) is NOT OK, as it allows going before the first page or after the last page