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="" />
        <figcaption id="imageDescription"></figcaption>

      </figure>

    </main>
              
            
!

CSS

              
                body {
  background-color: pink;
  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'

// ------ SAVING IN CONSTANTS THE DOM ELEMENTS WE NEED TO WORK WITH ------ //

// 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')

// here we create a link to the image description element, so we can change the displayed character name
const imageDescription = document.getElementById('imageDescription')

// ------ SAVING THE URL OF THE API WE WANT TO REQUEST DATA FROM ------ //
const API_URL = 'https://rickandmortyapi.com/api/character/:id'

var request = new XMLHttpRequest()

function getCharacter (id) {
  const URL = API_URL.replace(':id', id)
  request.open('GET', URL)
  request.setRequestHeader('X-Requested-With', 'XMLHttpRequest')
  request.responseType = 'json'
  request.onload = function () {
    // the response status tells us the HTTP status code, if it is 200, it means the response status is "OK"
    if (this.status === 200) {
      // we save the response into a variable
      var newCharacterJSON = this.response
      console.log(newCharacterJSON)

      // 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)

      // to get the image description, we access the "name" JSON key
      var characterName = newCharacterJSON.name
      if (characterName) {
      // we set the text to show the name we got from the response
        imageDescription.innerHTML = characterName
      } else {
        // if no input has been given, we display the message to choose one
        imageDescription.innerHTML = 'Please choose a number'
      }
    } else {
      // if the response status is different from 200, an error ocurred (the ID may be out of range)
      imageDescription.innerHTML =
        `An error occurred during your request! Choose a different number. Code: ${request.status}`
    }
  }
  request.send()
}

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

              
            
!
999px

Console