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

              
                 <main>
      <label for="number">Choose a number</label><br>
      <input id="number" type="number" min="1" max="671" />

      <figure>

        <img id="avatar" src="" alt="" />
     </figure>

    </main>
              
            
!

CSS

              
                body {
  background-color: coral;
  color: grey;
  font-size: xx-large;
  text-transform: uppercase;
  text-align: center;
  font-family: Verdana, Geneva, Tahoma, sans-serif;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  
}

              
            
!

JS

              
                'use strict'

// the inputValue allows us to access the value of the user's input, i.e. the chosen number
const inputValue = document.getElementById('number')

// this constant allows us to set the image to be displayed, by dynamically changing its "src" attribute
const avatar = document.getElementById('avatar')

const API_URL = 'https://rickandmortyapi.com/api/character/:id'

function getCharacter (id) {
  const URL = API_URL.replace(':id', id)
  fetch(URL)
  // first we attach the error handler function
    .then(handleErrors)

    // next, if there was no error, we use the json method on our response object
    .then(function (response) {
      console.log(response)
      return response.json()
    })
    // since the response.json() method resulst in another promise, we attach another then handler on the data we receive
    .then(function (data) {
      const newCharacterJSON = data
      // upon studying the response data structure, we know we need to access the "image" JSON key to get the image URL
      var imagePath = newCharacterJSON.image

      // now we set the src attribute of our image element to the URL from the reponse
      avatar.setAttribute('src', imagePath)
    })
    // this part of the code allows us to correctly catch the errors which might have appeared, both errors of the original promise being rejected as well as the errors which are thrown by our handleErrors function
    .catch(function (err) {
      console.log('Something went wrong!', err)
    })
}

inputValue.addEventListener('change', function () {
  const choosenNumber = inputValue.value
  getCharacter(choosenNumber)
})

function handleErrors (response) {  
    if (response.ok) {
       return response
}
     throw new Error(response.statusText) 
}   
              
            
!
999px

Console