Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="container card">
  <h2 class="mb-4">Chuck Norris jokes generator</h2>

  <form class="form-inline">
    <div class="form-group mx-sm-3 mb-2">
      <input type="text" class="form-control" placeholder="ID" />
    </div>
    <button type="submit" class="byId btn btn-primary mb-2">
      Get joke by ID
    </button>
  </form>
  <button type="button" id="random" class="byId btn btn-primary mx-sm-3 mb-2 mt-4">
     Or get a random Joke
    </button>

  <div id="joke" class="mt-3">
    Chuck Norris something...
  </div>
</div>;
              
            
!

CSS

              
                body {
  margin-top: 2rem;
}

#joke {
  border: 1px solid grey;
  border-radius: 4px;
  padding: 1rem;
  background-color: #ffffcf;
  text-align: center;
  font-size: 1.2rem;
}

.card {
  padding: 1rem;
  background-color: #dedbdb;
}
              
            
!

JS

              
                "use strict";

/*
Another exercise to repeat classes:

[ x ] 1. Build a form with an input field for an ID (number) and a submit button.
[ x ] 2. Write a class to call the Chuck Norris Jokes API and get the Joke with the given ID.
[ X ] 3. Add another button "Random joke" on your website that asks the API for a random joke.
[ x ] 4. Display the joke as a nice CSS-styled quote on the page.

API:
http://www.icndb.com/api/ 
*/
class ChuckNorrisApi {
  // Setting up the properties, we need in our class
  constructor() {
    this.apiUrl = "https://api.icndb.com/jokes";
    this.idEndpoint = "/{id}";
    this.randomEndpoint = "/random";

    // Registering the form events
    this.registerEvents();
  }

  registerEvents() {
    // Get the needed elements
    const form = document.querySelector(".form-inline");
    const button = document.querySelector("#random");
    const input = form.querySelector(".form-control");
    
    button.addEventListener("click", () => {
      
    const urlRandom = `${this.apiUrl}${this.randomEndpoint}`;
    console.log("URL", urlRandom);
      
    // Call the API and get joke data
    $.getJSON(urlRandom).then(data => {const randomJoke =  this.filterResults(data);
    console.log("Random joke", randomJoke);

     // Update the div element to have the new text inside
     document.querySelector("#joke").innerHTML = `<q>${randomJoke}</q>`;})
   
    })

    // Add the event, when the form is submitted
    form.addEventListener("submit", e => {
      // Prevent the default to stop sending the form to the URL by the browser
      e.preventDefault();

      // Get the id from the input field
      const id = input.value.trim();

      this.getJoke(id).then(response => {
        console.log(response);

        // Filter the joke to the data we need (as it is having too many properties)

        const joke = this.filterResults(response);
        
        console.log("Joke", joke)

        // Update the div element to have the new text inside
        document.querySelector("#joke").innerHTML = `<q>${joke}</q>`;
      }).catch(err => throw err);
    });
  }

  getJoke(id, callback) {
    // Create the whole URL to get the data
    const endpoint = this.idEndpoint.replace("{id}", id);
    const url = `${this.apiUrl}${endpoint}`;

    console.log("get data", url);
  // Return a promise with this jQuery
    return $.getJSON(url);
  }

  filterResults(data) {
    console.log("filter data", data);

    // Filter the joke property
    return data.value.joke;
  }
}


const jokeMachine = new ChuckNorrisApi();
console.log(jokeMachine);

              
            
!
999px

Console