Build a Quote Generator

Source Demo

By Ivan Chau


Getting Started

In this tutorial, we’ll use some simple JQuery connected with a Quote API to make a website present a quote after the spacebar is pressed. We will be using AJAX to service requests between the API and handling the JSONP response.

As always, you’ll need a folder with three files:

To start with, our content will be displayed in the center of the screen. We also want to display our quote and source/author separately. Create a parent container div and two other divs for the quote and source. Last, create some instructions at the tails of the site so people know how to use it, import JQuery scripts using CDN, and link a custom font for us to use from Google Fonts. Our website isn’t functional and it’s pretty sketchy right now so let’s use some CSS to alleviate parts of those problems.

      <meta charset="utf-8" />
      <meta name="viewport" content="width=device-width, initial-scale=1" />
      <link rel="stylesheet" href="style.css" />
      <link href="" rel="stylesheet" />
    <div class="contain">
      <div class="executed" id="quote">
      <div id="writer">
      <div class="bottom">
        <h2> press space to inspire </h2>

  <script src="" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script>
  <script src="app.js"></script>


The divs are empty, so you won’t see them. However, they still are there, but all you should see as a result is the instructions to press the space we typed earlier.

Styling the app

Here’s some basic things you should style:

This is my approach, feel free to tweak it to your preferences.

  html,body {
    height: 100%;
    width: 100%;
    background: #202020;
    overflow: hidden;
  /* font css */
  p {

  /* main container css */
    vertical-align: middle;
    text-align: center;

  /* quote css */
    font-family : Inconsolata;
    position: relative;

  /* source css */
    font-size: 1em;
    text-align: right;
    font-family : Inconsolata;
    position: relative;
    -webkit-transition: all 1s ease-in-out;
      -moz-transition: all 1s ease-in-out;
      -o-transition: all 1s ease-in-out;
      transition: all 1s ease-in-out;

  /* for the after stage of the source */
  .fade {
    opacity:0 !important;

  /* for the after stage of the quote.*/
  .executed {
    right: 0;
    -webkit-transition: all 2s ease-in-out;
      -moz-transition: all 2s ease-in-out;
      -o-transition: all 2s ease-in-out;
      transition: all 2s ease-in-out;

  /* for the before stage of the quote; should slide right when presented and left when removed.  */
  .reset {
    -webkit-transition: all 1s ease-in-out;
      -moz-transition: all 1s ease-in-out;
      -o-transition: all 1s ease-in-out;
      transition: all 1s ease-in-out;

  /* for the instructions at the bottom */
  .bottom {
    position: absolute;

Now that we’ve styled our app, we need to implement the true function of it. Let’s go ahead and do that.

The API function

To get the quotes, we need to call the Quotes API when the spacebar is pressed. Luckily, JQuery implements a window function just for that, known as keypress. To understand what to write, we have to go through what is happening in our site at that point. The current quote being displayed will be removed and we will request for a new one. When it returns from the API, we will display it.

Voila! May this be useful when you are lacking insipiration:

    $(window).keypress(function(e) {
      if (e.which === 32) {
            crossOrigin: true,
            url: "[orderby]=
        }, 1000); 
  function mycallback(json){
    var quote = json[0];

What’s next?

Your API is super powerful! Try making the site responsive and changing up the style. Try making other apps using JSON APIs like a weather app!