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

              
                <head>
  <!-- This is a javascript library called jQuery that is nice to use for ajax calls -->
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <!-- Importing Bootstrap, a styling library -->
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">
<!--  Importing FontAwesome, a library for icons -->
  <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.12/css/all.css" integrity="sha384-G0fIWCsCzJIMAVNQPfjH08cyYaUtMwjJwqiRKxxE/rx96Uroj1BtIQ6MLJuheaO9" crossorigin="anonymous">


  <!-- This is my own javascript -->
  <script src="apicall.js"></script>
</head>

<body>

  <div class="container">
    <h1>StarWarsAPI example</h1>

    <!--Here is where the magic happens! 
By using the onclick attribute for button 
we can make a call to our javascript function. 
Check it out in the apicall.js!  -->
    <button type="button" class="btn btn-primary" onclick="getData();">
      Get random character
      <i class="fa fa-sync"></i>
    </button>
    <div class="container data-holder invisible">
      <div>Name: <span id="name"></span></div>
      <div>Height: <span id="height"></span></div>
      <div>Mass: <span id="mass"></span></div>
      <div>Hair Color: <span id="hair_color"></span></div>
      <div>Skin Color: <span id="skin_color"></span></div>
      <div>Eye Color: <span id="eye_color"></span></div>
      <div>Birth Year: <span id="birth_year"></span></div>
      <div>Gender: <span id="gender"></span></div>
    </div>
  </div>

</body>
              
            
!

CSS

              
                body {
  background: #000
    url(https://miukimiu.github.io/star-wars-random-quotes/images/bg.jpg);
}

.container {
  color: #fff;
}

              
            
!

JS

              
                function getData() {
  //randomNumber is assigned a random value between 1 and 87 - that is how many star wars characters this API contains 
  const randomNumber = Math.floor(Math.random() * 87) + 1;
  //To use a function from the jQuery lib we just use $ then the function we want to call.
  $.ajax({  
    //This is the url to the api:
    url: "https://swapi.co/api/people/" + randomNumber,

    //This is where we handle the response from the API.
    //Data is just a name, you can call it "response" or anything you like!
    success: function(data) {
      //Find the div by using $ and the id of the the div.
      //Use JSON.stringify to transform the response data into text that the browser can handle. From the data object we pick out the properties we want to display on the page ex. name, height...
      $("#name").html(JSON.stringify(data.name));
      $("#height").html(JSON.stringify(data.height));
      $("#mass").html(JSON.stringify(data.mass));
      $("#hair_color").html(JSON.stringify(data.hair_color));
      $("#skin_color").html(JSON.stringify(data.skin_color));
      $("#eye_color").html(JSON.stringify(data.eye_color));
      $("#birth_year").html(JSON.stringify(data.birth_year));
      $("#gender").html(JSON.stringify(data.gender));
      $(".data-holder").removeClass('invisible');
    }
  });
}

              
            
!
999px

Console